从数据库获取所有订单 - 仅返回一行

时间:2017-09-28 15:46:40

标签: mysql sql select

我试图让表格链接在一起以返回电子邮件,订单号,日期,总成本和返回的项目数,但我期望返回的两行数据,只有一行。表格如下;

Table: Customer
+------------+---------------------+-----------------+
| CustomerID | EmailAddress        | DeliveryAddress |
+------------+---------------------+-----------------+
| BLF1       | FredBloggs@NT.co.uk | NULL            |
| SMJ2       | JoeSmith@NT.co.uk   | NULL            |
+------------+---------------------+-----------------+


Table: Item
+----------+-------------+-----------+
| ItemCode | Description | UnitPrice |
+----------+-------------+-----------+
| 1234     | Ring Binder |       1.5 |
| 3456     | Stapler     |      2.99 |
| 8967     | Divider     |       0.5 |
| 9684     | Scissors    |      1.99 |
+----------+-------------+-----------+


Table: ItemOrder
+-------------+----------+---------------+
| OrderNumber | ItemCode | OrderQuantity |
+-------------+----------+---------------+
| 012367      | 1234     |             3 |
| 012367      | 3456     |             1 |
| 012367      | 8967     |             4 |
| 034231      | 3456     |             4 |
| 034231      | 9684     |             2 |
+-------------+----------+---------------+


Table: OnlineOrder
+-------------+------------+------------+
| OrderNumber | CustomerID | OrderDate  |
+-------------+------------+------------+
| 012367      | BLF1       | 2009-05-01 |
| 034231      | SMJ2       | 2009-05-03 |
+-------------+------------+------------+

我运行的查询是:

SELECT Customer.EmailAddress, 
       OnlineOrder.OrderNumber, 
       OnlineOrder.OrderDate, 
       Count(Item.ItemCode) as TotalItems, 
       FORMAT(SUM(Item.UnitPrice*ItemOrder.OrderQuantity), 2) as TotalPrice
FROM Customer, 
     Item, 
     OnlineOrder, 
     ItemOrder
WHERE OnlineOrder.OrderNumber = ItemOrder.OrderNumber
AND Customer.CustomerID = OnlineOrder.CustomerID
AND Item.ItemCode = ItemOrder.ItemCode;`

这只返回一条记录;

+---------------------+-------------+------------+------------+------------+
| EmailAddress        | OrderNumber | OrderDate  | TotalItems | TotalPrice |      
+---------------------+-------------+------------+------------+------------+ 
| FredBloggs@NT.co.uk | 012367      | 2009-05-01 |          5 | 25.43      |
+---------------------+-------------+------------+------------+------------+

为什么查询会返回第二个订单,包含该订单的详细信息和购买者?

2 个答案:

答案 0 :(得分:1)

这是使用正确的join / on子句格式化的相同查询。我还更改了每个表的别名引用。特别是对于较大的表名,它可以显着简化可读性。

SELECT 
      C.EmailAddress, 
      OO.OrderNumber, 
      OO.OrderDate, 
      Count(I.ItemCode) as TotalItems, 
      FORMAT(SUM(I.UnitPrice * IO.OrderQuantity), 2) as TotalPrice
   FROM 
      OnlineOrder OO
         JOIN ItemOrder IO
            ON OO.OrderNumber = IO.OrderNumber
            JOIN Item I
               ON IO.ItemCode = I.ItemCode
         JOIN Customer C
            ON OO.CustomerID = C.CustomerID
   GROUP BY
      OO.OrderNumber

我添加的唯一内容是GROUP BY子句。由于您希望按订单计算总计,因此该组按订单排序。如果您希望总数为每位客户,则需要删除明确的订单编号和日期,以查看所有订单商品和所有订单项总计。

答案 1 :(得分:-1)

我使用ANSI连接复制了您的数据和SQL,得到了正确答案,两条记录。我不知道你做错了什么。

CREATE TABLE customer
(
    customerid VARCHAR (10)
  , emailaddress VARCHAR (50)
  , deliveryaddress VARCHAR (100)
);

INSERT INTO customer (
           customerid, emailaddress, deliveryaddress
            )
     VALUES (
                'BLF1', 'FredBloggs@NT.co.uk', NULL
            );

INSERT INTO customer (
           customerid, emailaddress, deliveryaddress
            )
     VALUES (
                'SMJ2', 'JoeSmith@NT.co.uk', NULL
            );

COMMIT;

CREATE TABLE item
(
    itemcode NUMBER
  , description VARCHAR (50)
  , unitprice DECIMAL (10, 2)
);

INSERT INTO item (
           itemcode, description, unitprice
            )
     VALUES (
                1234, 'Ring Binder', 1.5
            );

INSERT INTO item (
           itemcode, description, unitprice
            )
     VALUES (
                3456, 'Stapler', 2.99
            );

INSERT INTO item (
           itemcode, description, unitprice
            )
     VALUES (
                8967, 'Divider', .5
            );

INSERT INTO item (
           itemcode, description, unitprice
            )
     VALUES (
                9684, 'Scissors', 1.99
            );

COMMIT;

CREATE TABLE itemorder
(
    ordernumber VARCHAR (10)
  , itemcode INTEGER
  , orderquantity INTEGER
);

INSERT INTO itemorder (
           ordernumber, itemcode, orderquantity
            )
     VALUES (
                '012367', 1234, 3
            );

INSERT INTO itemorder (
           ordernumber, itemcode, orderquantity
            )
     VALUES (
                '012367', 3456, 1
            );

INSERT INTO itemorder (
           ordernumber, itemcode, orderquantity
            )
     VALUES (
                '012367', 8967, 4
            );

INSERT INTO itemorder (
           ordernumber, itemcode, orderquantity
            )
     VALUES (
                '034231', 3456, 4
            );

INSERT INTO itemorder (
           ordernumber, itemcode, orderquantity
            )
     VALUES (
                '034231', 9684, 2
            );

COMMIT;

CREATE TABLE onlineorder
(
    ordernumber VARCHAR (10)
  , customerid VARCHAR (10)
  , orderdate DATE
);

INSERT INTO onlineorder (
           ordernumber, customerid, orderdate
            )
     VALUES (
                '012367', 'BLF1', DATE '2009-05-01'
            );

INSERT INTO onlineorder (
           ordernumber, customerid, orderdate
            )
     VALUES (
                '034231', 'SMJ2', DATE '2009-05-03'
            );

COMMIT;

  SELECT customer.emailaddress
       , onlineorder.ordernumber
       , onlineorder.orderdate
       , COUNT (item.itemcode) AS totalitems
       , SUM (item.unitprice * itemorder.orderquantity) AS totalprice
    FROM customer
         INNER JOIN onlineorder ON customer.customerid = onlineorder.customerid
         INNER JOIN itemorder ON itemorder.ordernumber = onlineorder.ordernumber
         INNER JOIN item ON item.itemcode = itemorder.itemcode
--   WHERE onlineorder.ordernumber = itemorder.ordernumber
--     AND customer.customerid = onlineorder.customerid
--     AND item.itemcode = itemorder.itemcode
GROUP BY customer.emailaddress, onlineorder.ordernumber, onlineorder.orderdate;