mysql somehow group and then select from tag map -


the table:

| tag_id | article_id | |--------|------------| | 1      | 100        | |--------|------------| | 2      | 100        | |--------|------------| | 1      | 200        | |--------|------------| | 1      | 300        | 

how select 2 first rows

the logic: select articles have tag_id 1 , 2

in case article id 100

i use php filter results, mysql neater.

to article_ids have both tag_id 1 , 2 can use clause combined group clause limited having clause this:

select article_id  your_table tag_id in (1,2) group article_id having count(distinct tag_id) = 2; 

with sample table return article_id = 100.

note return articles don't have tag_id 1 , 2, articles have tag_id 1 , 2 plus other tags - limits result articles have at least tag_id 1 , 2 (but have more). if want limit articles have tag_id 1 , 2 add correlated exists predicate instance.

sample sql fiddle


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -