-1

I have a table in which one column is filled with data like 32;3;13;33;43

so

SELECT * FROM table; 

gives something like

name ids
vegetables 13;3;63
fruits 37;73;333

When I'm querying MySQL like

SELECT * FROM table WHERE ids LIKE '%3%'

it gives me both records but obviously I want only this containing 3.
How to query MySQL correctly?

Kornel
  • 4,184
  • 4
  • 28
  • 30
  • 3
    ...and that's one of the about 20 reasons why you should not store comma separated values in a column. See this answer: http://stackoverflow.com/a/3653574/447489 – fancyPants Mar 06 '14 at 11:18
  • It's not me. The data are there already. Sometimes world is not perfect you know? – Kornel Mar 06 '14 at 11:24

3 Answers3

1

Try to use:

SELECT * FROM table WHERE CONCAT(';',ids,';') LIKE '%;3;%'
valex
  • 23,966
  • 7
  • 43
  • 60
1

You will need to cover the case where it's the first in the list and the last.

SELECT * FROM table WHERE ids LIKE '%;3;%' OR LIKE '%;3' OR LIKE '3;%'
stuartf
  • 648
  • 4
  • 8
0

You can use FIND_IN_SET, if you replace the ; with a , before checking the value:0

SELECT * 
FROM table 
WHERE FIND_IN_SET('3', REPLACE(ids, ';', ','))
Kickstart
  • 21,403
  • 2
  • 21
  • 33