0

Possible Duplicate:
Searching a column containing CSV data in a MySQL table for existence of input values
MySQL query finding values in a comma separated string

I have two tables. One table includes tags, the other table includes posts which have tags in a comma separated string. I need to get all posts with a specific tag id.

I tried this but it didn't work:

SELECT
ab_tags.id,
ab_tags.lang,
ab_tags.tag,
ab_tags.slug,
ab_etkinlik.title,
ab_etkinlik.tag
FROM
ab_tags ,
ab_etkinlik
WHERE
ab_tags.id = 2
ab_tags.id IN (ab_etkinlik.tag)

ab_tags.id is coming from $_get val
ab_etkinlik.tag is a string like "2,4,8,20,48"

thanks in advance.

Community
  • 1
  • 1
mrtakdnz
  • 149
  • 2
  • 5
  • 10
  • 4
    Don't store comma separated values in a relational database. – Martin Smith Sep 03 '11 at 10:21
  • In what way didn't it work? Please describe observed behaviour, including any error message you get. Also, which flavour of RDBMS are you using? MySQL is the usual choice with PHP but it helps us to know for sure. – APC Sep 03 '11 at 10:23

2 Answers2

2

If you designed this schema yourself, you're missing the concept of a link-table. If you have tables called 'tags' and 'events' (etkinlik), you should probably have a two-column 'eventtags' link-table with zero-to-many rows for each event. First column is event id, second column tag id.

searlea
  • 8,173
  • 4
  • 34
  • 37
1

Try this:

SELECT ab_tags.id,
       ab_tags.lang,
       ab_tags.tag,
       ab_tags.slug,
       ab_etkinlik.title,
       ab_etkinlik.tag
FROM ab_etkinlik
     JOIN ab_tags ON ab_tags.tag IN(ab_etkinlik.tag)
WHERE ab_tags.id = 2
Narf
  • 14,600
  • 3
  • 37
  • 66