sql - Using relational division in mysql on a simple associative table -


we have associative table connects threads tags. trying figure out query give thread id's tied combination of tag id's. created sqlfiddle at:

http://sqlfiddle.com/#!9/c8ebe/3

so table structured this:

 create table `threads_tags` (   `id_threads` bigint(20),   `id_tags` bigint(20) ); 

some sample data may this:

id_threads   id_tags   2             12   4             12   9             10   2             21   3             2   2             1   5             1   5             21 

in our example, have tag id's 12 , 1 , need query return 2 id_threads because id_threads has both id_tags = 12 , id_tags = 1. there no limit number of tags may need match against. think need using relational division , tried using query:

select     id_threads     threads_tags tt1     id_tags in (1,12) group     id_threads having     count(*) = (         select             count(*)                     threads_tags tt2                     id_threads = tt1.id_threads     ) 

it doesn't seem working though. thought using solution @ relational division in mysql without aggregrate functions? in example, not sure put list of id_tags want input query. new @ relational division type of queries can give me here appreciated.

this should you're looking for:

select id_threads threads_tags     id_tags in (1,12)  group id_threads  having count(distinct id_tags) = 2; 

the number 2 after having count... number of values in list (in case it's 2 - 1 , 12).