使用ON关键字的正确mySQL语法?

时间:2013-07-31 09:53:17

标签: mysql sql database sql-order-by

提出的问题是:

售价50美元或以下的二手书籍的标题,作者姓名和价格是多少?结果应按价格按降序排序,然后按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:

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

3 个答案:

答案 0 :(得分:2)

您的查询需要返工才能正确使用ON子句。假设所有三个表都存在isbn列,这里是查询:

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;

我希望有所帮助。

答案 1 :(得分: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;

好的做法是使用别名,请在下面的示例

中使用
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;

答案 2 :(得分:1)

在mySQL中的ON语句是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;