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