2014-05-08 191 views
0

所以我試着如下爲「列出所有由作家蓋爾書面和由袖珍書印刷書籍」SQL查詢忽略條件

我的數據庫表是

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 

這是我的查詢,它忽略了我的大風條件,只是得到所有由袖珍書出版的書籍,有什麼幫助?

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; 

回答

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 

而且你也不需要分組。

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;`