我已经在这个问题上工作了大约 8 个小时,但没有成功。有没有人有任何想法?
问题:使用正确的表,使用您希望的连接操作创建一个子查询,该操作将列出客户编号、名字和姓氏连接在一起,以及所有订购最昂贵书籍的客户的城市(基于零售价格)。给出组合的客户名称列和“客户名称”的别名。
这是我的代码:
SELECT book_customer.customerid, lastname || ', ' || firstname AS "Customer Name", city
FROM book_customer, book_order, order_items
WHERE book_customer.customerid = book_order.customerid
AND book_order.orderid = order_items.orderid
AND bookid =
(
SELECT MAX(retail)
FROM books
GROUP BY bookid
)
;
结果:
SQL> SELECT book_customer.customerid, lastname || ', ' || firstname AS "Customer Name", city
2 FROM book_customer, book_order, order_items
3 WHERE book_customer.customerid = book_order.customerid
4 AND book_order.orderid = order_items.orderid
5 AND bookid =
6 (
7 SELECT MAX(retail)
8 FROM books
9 GROUP BY bookid
10 )
11 ;
SELECT MAX(retail)
*
ERROR at line 7:
ORA-01427: single-row subquery returns more than one row
我也试过这个:
SELECT customerid, lastname || ', ' || fistname AS "Customer Name", city
FROM book_customer, book_order
WHERE book_customer.customerid = book_order.customerid
AND orderid =
(
SELECT orderid
FROM order_items
AND bookid =
(
SELECT bookin
FROM books
GROUP BY bookid
HAVING MAX(retail)
)
)
;
结果:
SQL> SELECT customerid, lastname || ', ' || fistname AS "Customer Name", city
2 FROM book_customer, book_order
3 WHERE book_customer.customerid = book_order.customerid
4 AND orderid =
5 (
6 SELECT orderid
7 FROM order_items
8 AND bookid =
9 (
10 SELECT bookid
11 FROM books
12 GROUP BY bookid
13 HAVING MAX(retail)
14 )
15 )
16 ;
AND bookid =
*
ERROR at line 8:
ORA-00907: missing right parenthesis
ERD
Book_Customer TABLE
CustomerID PK
Lastname
Firstname
Address
City
State
Zip
Referred
Book_Order TABLE
OrderID PK
CustomerID FK
OrderDate
ShipDate
ShipStreet
ShipCity
ShipState
ShipZip
Order_Items TABLE
OrderID PK/FK
ItemNum PK/FK
BookID
Quantity
Books TABLE
BookID PK
ISBN
Title
PubDate
PubID FK
Cost
Retail
Category
任何帮助将非常感激。