PDA

View Full Version : mySQL intersect?


Andreas
06-02-2005, 05:12 PM
Let's say i've got a table that has a column with comma separated IDs.
Example:

columname
---------------
1,2,5
4,7,8
3,6,11

Now I have a string with comma separated IDs: 2,8

Any ideas for a query to get rows that contain at least one ID from this string in column columnname?

Revan
06-02-2005, 05:14 PM
This is me talking untested stuff again, but what I would do is split the string and use LIKE '$string,'. Dunno if this would fit your purpose as every 3rd number seems to be lacking their comma (and breaking the LIKE if those numbers will be searched for), else it might work.

Andreas
06-02-2005, 05:19 PM
LIKE is a bad idea I think as
SELECT '23,24,25' LIKE '%2%'
does match

What actually does work is
SELECT * FROM table WHERE FIND_IN_SET('2', columnname) OR FIND_IN_SET('8', columnname)
but I feel that this is not a good way to do this.