-1

提出的问题是这样的:

价格在 50.00 美元或以下的二手书的书名、作者姓名和价格是什么?结果应按价格降序排列,然后按 AZ 顺序排列标题。

代码:

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
4

3 回答 3

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;

我希望这会有所帮助。

于 2013-07-31T10:03:50.237 回答
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;
于 2013-07-31T09:54:04.380 回答
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;
于 2013-07-31T09:57:09.313 回答