tags:

views:

120

answers:

3

Hi

I have a table like this:

UID(int) NUMBERS(blob)
----------------------
1        1,13,15,20
2        3,10,15,20
3        3,15

And I would like to test if 3 and 15 are in the blob called NUMBERS. And can see the LIKE %% cannot be used

Only row with ID 2 and three scoulb be selected...

A: 

Not the most pretty solution, but it works:

select
   UID
from
   YOUR_TABLE
where
   find_in_set('3', cast(NUMBERS as char)) > 0
   and
   find_in_set('15', cast(NUMBERS as char)) > 0

Note that it's string comparison, so you may need to cast your input parameters to char as well.

Björn
Thanks for your help. I found a page with an exmaple for seaching comma separated values. I posted it as an answer. Guess your solution would work fine too
Tillebeck
+1  A: 

This one also works:

SELECT * FROM table WHERE 3 IN (NUMBERS) AND 15 IN (NUMBERS)

using the IN will look into a comma separated string eg. these two

WHERE banana IN ('apple', 'banana', 'coconut')
WHERE 3 IN (2,3,6,8,90)

Based on info found on this page: http://www.webdevelopersnotes.com/tutorials/sql/tutorial_mysql_in_and_between.php3

Tillebeck
Of course it does, stupid me!
Björn
feel bad about accepting own answer since the other was right too. But the IN seems more right than the find_in_set even though that would work too.
Tillebeck
+1  A: 

Also check if this is helpful to anyone

An Extended function to eliminate the limitation of native FIND_IN_SET() in MySQL, this new extended version FIND_IN_SET_X() provides feature to compare one list with another list.

i.e.

mysql> SELECT FIND_IN_SET_X('x,c','a,b,c,d'); -> 3

http://forge.mysql.com/tools/tool.php?id=290

Nilesh
Thanks for posting. Didn't know that function
Tillebeck