Mysql加入多个表排除某些记录

时间:2013-01-22 04:49:01

标签: mysql join multiple-tables records

我在MySQL方面不是很好,所以需要一些帮助。我有三个表作者(author_id,name),book(book_id,name),publisher(id,author_id,book_id)

我想只获取那些只在rdbms上编写/发布书籍的作者。

我试过了:

SELECT a.author_id, a.name, b.name as bookname
FROM author a
LEFT JOIN publisher p ON p.author_id = a.author_id
INNER JOIN book b ON b.book_id = p.book_id ANd b.name='rdbms'

给了我所有在rdbms上写过书的作者以及其他也写过其他一些主题的书的作者,所以我想把它们排除在外。

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT a.author_id, a.name, b.name as bookname
FROM author a
LEFT JOIN publisher p ON p.author_id = a.author_id
INNER JOIN book b ON b.book_id = p.book_id
group by a.author_id
HAVING count(p.book_id) = 1 AND bookname='rdbms'