SQL查询忽略条件

时间:2014-05-08 12:05:15

标签: mysql sql database sqlite

所以我试图“列出由作者Gale编写并由Pocket Books打印的所有书籍”

我的数据库表格如下

Table: author 
authorNumber, authorName
         01         Gale
         02         Ben
Table: book
bookCode,  bookTitle,     publishCode
     189   Press & Trash  PB
    3743   Ecommerce      PB

Table: publisher
publisherCode, publisherName
      PB        Pocket Books
      BB        Batman Books

Table: wrote
bookCode,  authorNumber
     189     01
     3743    02 

这是我的查询,它忽略了我的Gale条件,只是让Pocket Books出版的所有书籍都有帮助吗?

SELECT bookTitle
FROM Book B, Wrote BC, Author, Publisher PC
WHERE B.bookCode = BC.bookCode
AND B.publisherCode = PC.publisherCode
AND authorName ='Gale'
AND publisherName ='Pocket Books'
GROUP BY bookTitle;

2 个答案:

答案 0 :(得分:1)

您忘记为author添加加入条件。您最好使用显式连接语法,使其更加明显。

SELECT B.bookTitle
FROM Book B
join Wrote BC on B.bookCode = BC.bookCode
join Author A on BC.authorNumber = A.authorNumber
join Publisher PC on B.publisherCode = PC.publisherCode
WHERE A.authorName ='Gale'
AND PC.publisherName ='Pocket Books'
GROUP BY B.bookTitle

你也不需要分组。

答案 1 :(得分:0)

您缺少一个加入BC.authorNumber=Author.authorNumber

SELECT bookTitle
FROM Book B, Wrote BC, Author, Publisher PC
WHERE B.bookCode = BC.bookCode
AND B.publisherCode = PC.publisherCode
AND BC.authorNumber=Author.authorNumber
AND authorName ='Gale'
AND publisherName ='Pocket Books'
GROUP BY bookTitle;`