如何修改我编写的查询以显示所需的结果?

时间:2012-05-31 05:13:13

标签: sql oracle plsql

使用BOOK_ORDER,ORDER_ITEMS和BOOKS表,使用OUTER JOIN操作创建一个查询,该操作将列出BOOKS表中所有书籍的书名,订单日期和订单号。按ORDER_ITEMS.BOOKID降序排序输出。有三本从未订购过的书应该出现在你的商品的顶部。

我写了这个查询: -

SELECT B.TITLE, BO.ORDERDATE, ORD.ORDERID FROM BOOKS B, BOOK_ORDER BO, ORDERITEMS ORD
WHERE B.BOOKID = ORD.BOOKID
AND BO.ORDERID = ORD.ORDERID(+)
ORDER BY ORD.ORDERID DESC

我得到了结果,但对以下部分问题感到困惑: -

“有三本书从未被订购过,应该会出现在您的商家信息的顶部。”

我猜这意味着我需要将那些具有Orderdate和OrderId的书显示为NULL或空白。但是如何在结果集之上获取这些行?我应该在我写的查询中更改什么?

这是BOOKS表: -

Name                                      Null?    Type
----------------------------------------- -------- ---------------
BOOKID                                    NOT NULL NUMBER(15)
ISBN                                               VARCHAR2(10)
TITLE                                              VARCHAR2(30)
PUBDATE                                            DATE
PUBID                                              NUMBER(2)
COST                                               NUMBER(5,2)
RETAIL                                             NUMBER(5,2)
CATEGORY                                           VARCHAR2(12)

这是ORDER_ITEMS表: -

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------
 ORDERID                                   NOT NULL NUMBER(4)
 ITEMNUM                                   NOT NULL NUMBER(2)
 BOOKID                                    NOT NULL NUMBER(15)
 QUANTITY                                           NUMBER(3)

这里是BOOK_ORDER表结构: -

 Name                                      Null?    Type
 ----------------------------------------- -------- --------------
 ORDERID                                   NOT NULL NUMBER(4)
 CUSTOMERID                                         NUMBER(4)
 ORDERDATE                                          DATE
 SHIPDATE                                           DATE
 SHIPSTREET                                         VARCHAR2(20)
 SHIPCITY                                           VARCHAR2(20)
 SHIPSTATE                                          VARCHAR2(2)
 SHIPZIP                                            VARCHAR2(5)

3 个答案:

答案 0 :(得分:3)

试试这个:

SELECT B.TITLE,
       BO.ORDERDATE,
       ORD.ORDERID
FROM BOOKS B,
     BOOK_ORDER BO,
     ORDER_ITEMS ORD
WHERE B.BOOKID = ORD.BOOKID(+)
  AND BO.ORDERID(+) = ORD.ORDERID
ORDER BY ORD.ORDERID DESC

顺便说一句,恕我直言,最好不要使用(+)运算符,而是使用ANSI JOIN关键字:

SELECT B.TITLE,
       BO.ORDERDATE,
       ORD.ORDERID
FROM BOOKS B
LEFT OUTER JOIN ORDER_ITEMS ORD ON B.BOOKID = ORD.BOOKID
FULL OUTER JOIN  BOOK_ORDER BO ON BO.ORDERID = ORD.ORDERID
ORDER BY ORD.ORDERID DESC

here是一个小提琴

答案 1 :(得分:0)

我认为这会显示未在顶部订购的书籍

SELECT B.TITLE, BO.ORDERDATE, ORD.ORDERID FROM BOOKS B, BOOK_ORDER BO, ORDERITEMS ORD
WHERE B.BOOKID = ORD.BOOKID
AND BO.ORDERID = ORD.ORDERID(+)
ORDER BY nvl(ORD.ORDERID,0)

试试这个

答案 2 :(得分:0)

在开场陈述中,它声明“按ORDER_ITEMS.BOOKID降序排序您的输出”。给出的示例尽管有效,但不要使用此请求的命令/查询。

我建议使用以下脚本:

SELECT Books.Title, Book_Order.OrderDate, Order_Items.OrderID
FROM Books, Book_Order, Order_Items
WHERE Books.BookID = Order_Items.BookID(+)
AND Book_Order.OrderID(+) = Order_Items.OrderID
ORDER BY Order_Items.BookID DESC;