Link to home
Start Free TrialLog in
Avatar of dev09
dev09

asked on

MySQL PHP search - best practice

Hi,

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?

Thanks
Paul
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Julian Hansen
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
12345,23456
123456,123

Open in new window

So to refine it you could do
SELECT * FROM serials 
  WHERE 
    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

Avatar of dev09
dev09

ASKER

Many thanks Ray and Julian. I like your way best Ray - it seems the most robust and accurate!