【发布时间】:2012-11-28 22:43:08
【问题描述】:
我有三张桌子:
author (columns: aut_id, aut_name)
book (columns: book_id, book_title)
authorbook (linking table, columns: aut_id, book_id)
每位作者都可以与一本书或多本书相关联。
每本书都可以与一位或多位作者相关联。
我想按姓名和作者的确切人数来选择一本书。
表结构:
author
aut_id | aut_name
1 Aname
2 Bname
3 Cname
book
book_id | book_title (the titles are identical on purpose)
1 Atitle
2 Atitle
3 Atitle
authorbook
aut_id | book_id
1 1
1 2
2 2
1 3
2 3
3 3
这是我的代码(为了更好的说明,我省略了作者表):
SELECT authorbook.book_id
FROM authorbook
INNER JOIN book
ON authorbook.book_id = book.book_id
WHERE book_title='Atitle'
AND FIND_IN_SET (authorbook.aut_id,'1,2')
GROUP BY authorbook.book_id
HAVING (COUNT(authorbook.aut_id)=2)
问题:此代码不仅返回所需的authorbook.book_id(2) 和两个authorbook.aut_ids (1,2),还返回authorbook.book_id(3) 和三个authorbook.aut_ids (1,2,3)。
问题:我如何SELECT 与FIND_IN_SET 子句中的作者(且没有其他作者)完全相关联的书?
非常感谢你的帮助!
【问题讨论】:
标签: mysql sql select group-by having-clause