我现在的数据库结构是:
posts:
id title
posts_tags
id post_id tag_id
tags:
id name现在想获得tags包含"a"且包含"b"但不包含"c"的posts列表,请问要怎么写呢?自己试了很久都不行

解决方案 »

  1.   

    select D.*
    from posts_tags A,posts D
    where tag_id=(select id from tags where name='a') and exists (select 1 from posts_tags B where A.post_id=B.post_id and B.tag_id=(select id from tags where name='b') )
    and not exists (select 1 fro posts_tags C where A.post_id=C.post_id and C.tag_id=(select id from tags where name='c'))
    and A.post_id = D.id