因此,我试图列出“由作者Gale撰写并由袖珍书印刷的所有书籍”

我的数据库表如下

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;

最佳答案

您忘记为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

而且您也不需要分组。

关于mysql - SQL查询忽略条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23541241/

10-09 02:28