MySQL PHP search - best practice


I have a field in a table 'serials' that has comma delimited content.  e.g.  12345,23456,34567
This field in each row does not contain the same numbers.  So there are no overlaps of numbers.

If i was to perform an SQL query on the table to give the result of which record has a certain number, what would be the best query string to use?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
There are delimiters in the fields, so you may be able to use a FULLTEXT index to search these, but it might also be worth refactoring to move the "serials" into another table.  That table would store the AUTO_INCREMENT id of the row of the original table, along with one of the serials in each row.  When you need to find all of the serials or find by serial, you would use a JOIN clause.

The stuff you need to know is in here.  You can look at your data and see if it is suitable in its current format:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Assuming you cannot follow Ray's solution of redesigning the table then LIKE would be the simplest option

SELECT * FROM serials WHERE field LIKE '%12345%'

Open in new window

The problem with this is that it will find records with the following numbers

Open in new window

So to refine it you could do
SELECT * FROM serials 
    field LIKE '12345,%' OR -- Starts with
    field LIKE '%,12345' OR -- Ends with
    field LIKE '%,12345,%' OR  -- In the middle
    field = '12345' -- On its own

Open in new window

dev09Author Commented:
Many thanks Ray and Julian. I like your way best Ray - it seems the most robust and accurate!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.