MySQL: relational division - use case -


can't seem puzzle different kind of information want here goes..

three different tables:

 entities id    name  1       2      b  3      c 
 tags id    name  1    tag_1  2    tag_2  3    tag_3 
 users id    name  1    user_1  2    user_2 

both entities:tags , users:tags 1:n relationships both entities , users having number of tags. there 2 tables handle relations; entities_tags , users_tags. example:

 entities_tags entity_id    tag_id  1              1  1              2  2              1  2              2  2              3  3              1 
 users_tags user_id    tag_id  1            1  1            2 

now, given user, i'd fetch entities tags included in users tags. in example user_1 should fetch entities , c. can fetch user tags separately if needed, shown below.

i've tried

select entities.*     (select group_concat(entities_tags.name separator ', ')) tags entities left outer join entities_tags on entities_tags.entity_id = entities.id left outer join tags on tags.id = entities_tags.tag_id tags.name in ('tag_1','tag_2') group entities.id having count(tags.name) = 2 

but give me both , b , not c. shouldn't b, should c.


Comments