开发者

Select values from a comma separated string [duplicate]

开发者 https://www.devze.com 2023-04-02 19:42 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Searching a column containing CSV data in a MySQL table for existence of input values
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Searching a column containing CSV data in a MySQL table for existence of input values

MySQL query findi开发者_Go百科ng 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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消