0

I'm trying to do this:

SELECT * FROM library WHERE $cat IN (tags);

Say that I have $cat = '15'; and tags is a fieldname in library that looks like this: 2,15,34, 45 How can I get it to work? I understand that normally the $var should be after the IN ($var), but the imploded array sits in my DB and not in the $var - somehow I've gotten it the wrong way around and I just cannot figure it out - Please help me, I'm wasting days :)

3 Answers3

1

You can use FIND_IN_SET instead, like this:

SELECT * FROM library
WHERE FIND_IN_SET($cat, tags);
a'r
  • 35,921
  • 7
  • 66
  • 67
1

select * from library where tags like '%,$cat,%';

Beofett
  • 2,388
  • 1
  • 23
  • 37
  • This doesn't work when the required tag is at the start or the end of the list. – a'r Mar 17 '11 at 16:41
  • You can use this trick to get around it: `CONCAT(',',tags,',') LIKE '%,$cat,%'`, but in MySQL, `FIND_IN_SET` is much easier. – a'r Mar 17 '11 at 16:55
0

Would you not do:

SELECT * FROM library WHERE tags = $cat
anothershrubery
  • 20,461
  • 14
  • 53
  • 98