2013-07-31 33 views
-1

提到的問題是這樣的:使用ON關鍵字的正確的mySQL語法?

售價低於50.00美元或以下的二手書籍的書名,作者姓名和價格是多少?結果應按價格降序排序,然後按A-Z順序標題。

CODE:

SELECT book.title, author.LastName, author.firstName, Ownersbook.price 
FROM book, author, ownersbook 
ON book.isbn = bookauthor.isbn 
WHERE Ownersbook.price < 50 
ORDER BY Ownersbook.price DESC, book.title ASC; 

我想表看起來像這樣:

+-------------------------------------------------+------------+-----------+-------+ 
| title           | lastname | firstname | price | 
+-------------------------------------------------+------------+-----------+-------+ 
| ER, SOM, NF, DK/NF, SQL, JDBC, ODBC, and RELVAR | Stratton | Bill  | 50.00 | 
| My Love's Last Longing       | Heartthrob | Danielle | 50.00 | 
| How to Keep your Cable Bill Down    | Hartpence | Bruce  | 45.00 | 
| Yes! Networking is for Bills Fans    | Lutz  | Peter  | 40.00 | 
| Yes! Networking is for Bills Fans    | Phelps  | Andrew | 40.00 | 
| Yes! Networking is for Bills Fans    | Leone  | James  | 40.00 | 
| The Shortest Book in the World     | Phelps  | Andrew | 35.00 | 
| How to Keep your Cellular Bill Down    | Hartpence | Bruce  | 25.00 | 
| My Lost Love's Long Last Lingering    | Heartthrob | Danielle | 25.00 | 
| From the Shores of Lake Erie to IT    | Stratton | Bill  | 0.00 | 
+-------------------------------------------------+------------+-----------+-------+ 
10 rows in set (0.00 sec) 

我試圖擺脫ON關鍵字聲明的,但它只是複製了大量的數據永遠和我不要那樣。我不確定ON關鍵字是如何正確使用的。

錯誤:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 'ON bo 
ok.isbn = bookauthor.isbn 
WHERE Ownersbook.price < 50 
ORDER BY book.title' at line 2 
+1

當你在FROM子句中列出這樣的'FROM book,author,ownersbook'時,它們是'CROSS JOIN'ed,這就是爲什麼你會得到重複。您必須在WHERE子句中寫入JOIN條件而不是ON子句。否則,使用JOIN語法使用INNER JOIN或任何其他具有ON子句的連接類型來編寫條件 –

回答

1

不能使用ON用關鍵字進行聯接,你應該把你的條件where條款

SELECT book.title, author.LastName, author.firstName, Ownersbook.price 
FROM book, author, ownersbook 
WHERE Ownersbook.price < 50 
and book.isbn = bookauthor.isbn 
-- here you have to add condition with ownersbook and some table 
-- here you have to add condition with bookauthor and some table 
ORDER BY Ownersbook.price DESC, book.title ASC; 

好的做法是使用別名,請consdier下面的例子

SELECT b.title, a.LastName, a.firstName, ob.price 
FROM book b, author a ownersbook ob, bookauthor ba 
WHERE ob.price < 50 
and b.isbn = ba.isbn 
-- here you have to add condition with ownersbook and some table 
-- here you have to add condition with bookauthor and some table 
ORDER BY ob.price DESC, b.title ASC; 
+0

任何解釋或應該只是採用(缺少)這個詞? –

1

ON語句在mySQL中是一個補語nt爲LEFT JOIN

我會嘗試這個

SELECT book.title, author.LastName, author.firstName, Ownersbook.price 
FROM book, author, ownersbook 
WHERE Ownersbook.price < 50 
AND book.isbn = bookauthor.isbn 
ORDER BY Ownersbook.price DESC, book.title ASC; 
2

您的查詢需要返工正確使用ON子句。假設在所有三個表中都存在列列,以下是查詢:

SELECT book.title, author.LastName, author.firstName, Ownersbook.price 
FROM 
    book 
inner 
    join author 
ON book.isbn = author.isbn 
inner 
    join Ownersbook 
ON book.isbn = Ownersbook.isbn 
WHERE Ownersbook.price < 50 
ORDER BY Ownersbook.price DESC, book.title ASC; 

我希望有所幫助。

相關問題