0

使用 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)
4

4 回答 4

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

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

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

是一个小提琴

于 2012-05-31T06:05:10.193 回答
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)

试试这个

于 2012-05-31T05:57:01.497 回答
0

也许这会有所帮助


SELECT DISTINCT B.TITLE,
                (CASE
                  WHEN B.BOOKID = ORD.BOOKID THEN
                   BO.ORDERDATE
                  ELSE
                   NULL
                END) AS ORDERDATE,
                (CASE
                  WHEN B.BOOKID = ORD.BOOKID THEN
                   ORD.ORDERID
                  ELSE
                   NULL
                END) AS ORDERID
  FROM SO_BOOKS B, SO_BOOK_ORDER BO, SO_ORDER_ITEMS ORD
 WHERE (B.BOOKID = ORD.BOOKID AND BO.ORDERID = ORD.ORDERID)
    OR (B.BOOKID NOT IN
       (SELECT DISTINCT ORD.BOOKID FROM SO_ORDER_ITEMS ORD))
 ORDER BY ORDERID DESC

于 2012-05-31T06:26:41.513 回答
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;
于 2015-08-17T00:21:20.010 回答