0

Field value - 29,21,30,28,32,35,49,50,137

SELECT *
FROM `smartspace_pickups`
WHERE `cat_id` LIKE '%37%'    
LIMIT 0 , 30 

I have field in which the ids are stored comma separated now i tried to fetch data using the above query but it giving me the values with the ID 137 and 371. I only want the value with the id 37 in the string.

3 Answers 3

2
SELECT *
FROM `smartspace_pickups`
WHERE FIND_IN_SET('37',`cat_id`)
LIMIT 0 , 30 
Sign up to request clarification or add additional context in comments.

Comments

2

you can use FIND_IN_SET(str,strlist)

SELECT *
FROM `smartspace_pickups`
WHERE FIND_IN_SET('37', `cat_id`)  > 0
LIMIT 0 , 30

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

Comments

0

make your ids field like this (add comma to the starting and ending)

,29,21,30,28,32,35,49,50,137,

and change your query to

LIKE '%,37,%' 

3 Comments

And if there is only one value in field?
@CORRUPT: will be like ,1,
Then WHERE CONCAT(',', cat_id, ',') LIKE '%,37,%' should work. Yea)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.