如何在MySQL中使用此表数据获得以下结果?

时间:2019-06-11 18:35:54

标签: mysql

我想从下表中获取结果,并将来自所有三个表的结果数据合并到一个结果集中。 我可以使用下面的查询获取一半的记录,但不是完整的结果。 我想了解如何在这种问题中获取结果数据?

我在下面的查询中尝试了此方法,但它应该是一个查询-

SELECT o.Customer, sum(o.orderPrice) as TotalValue 
FROM orders_online o
GROUP BY o.Customer , o.Country
HAVING o.Country = 'UK' or o.Country = 'US'and TotalValue > 1000
order by o.Customer ASC


SELECT  sum(o.orderPrice) as TotalReturnValue,o.Customer
FROM orders_online o 
right join online_returns orr on o.OrderId = orr.OrderId
GROUP BY o.Customer 
Union 
SELECT  sum(o.orderPrice) as TotalReturnValue,o.Customer
FROM orders_online o 
right join store_returns sr on o.OrderId = sr.OrderId
where o.Country IN('UK','US')
GROUP BY o.Customer 
order by Customer ASC



CREATE TABLE `online_returns` (
  `OrderId` int(11) NOT NULL,
  `ReturnDate` date NOT NULL,
  `Customer` varchar(255) NOT NULL,
  `Reason` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `online_returns` (`OrderId`, `ReturnDate`, `Customer`, `Reason`) VALUES
(2, '2019-06-20', 'Nilsen', 'S'),
(3, '2019-06-20', 'Nilsen', 'AA');

CREATE TABLE `orders_online` (
  `OrderId` int(11) NOT NULL,
  `OrderDate` date NOT NULL,
  `OrderPrice` int(11) NOT NULL,
  `Customer` varchar(255) NOT NULL,
  `Country` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `orders_online` (`OrderId`, `OrderDate`, `OrderPrice`, `Customer`, `Country`) VALUES
(1, '2019-06-05', 1000, 'Hansen', 'UK'),
(2, '2019-06-05', 1600, 'Nilsen', 'US'),
(3, '2019-06-11', 700, 'Nilsen', 'US'),
(4, '2019-06-12', 300, 'Hansen', 'UK'),
(5, '2019-06-17', 200, 'Kirk', 'US'),
(6, '2019-06-18', 2000, 'Jensen', 'FR'),
(7, '2019-06-20', 100, 'Nilsen', 'US'),
(8, '2019-06-26', 1200, 'Tim', 'UK');

CREATE TABLE `store_returns` (
  `OrderId` int(11) NOT NULL,
  `ReturnDate` date NOT NULL,
  `Customer` varchar(255) NOT NULL,
  `Reason` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `store_returns` (`OrderId`, `ReturnDate`, `Customer`, `Reason`) VALUES
(4, '2019-06-22', 'Hansen', 'R'),
(6, '2019-06-26', 'Jensen', 'R');

ALTER TABLE `orders_online`
  ADD PRIMARY KEY (`OrderId`);

ALTER TABLE `orders_online`
  MODIFY `OrderId` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

结果应该在一个查询中。 只有三行。

enter image description here

2 个答案:

答案 0 :(得分:0)

加入两个查询。

您还需要围绕UNION进行包装查询,以将每个客户的两个退货表中的结果合并在一起。而且您必须使用UNION ALL,以免删除重复项。

UNION中的子查询不需要使用RIGHT JOIN。它们可以使用INNER JOIN,因为我们将它们与使用LEFT JOIN的第一个查询结合在一起。

SELECT t1.Customer, t1.TotalValue, t2.TotalReturnValue, t2.ReturnCount
FROM (
    SELECT o.Customer, sum(o.orderPrice) as TotalValue 
    FROM orders_online o
    WHERE o.Country IN ('UK', 'US')
    GROUP BY o.Customer , o.Country
    HAVING TotalValue > 1000
) AS t1
LEFT JOIN (
    SELECT Customer, SUM(TotalReturnValue) AS TotalReturnValue, SUM(ReturnCount) AS ReturnCount
    FROM (
        SELECT o.Customer, sum(o.orderPrice) as TotalReturnValue, COUNT(*) AS ReturnCount
        FROM orders_online o 
        join online_returns orr on o.OrderId = orr.OrderId
        WHERE o.Country IN ('UK', 'US')
        GROUP BY o.Customer 

        Union ALL

        SELECT o.Customer, sum(o.orderPrice) as TotalReturnValue, COUNT(*) AS ReturnCount
        FROM orders_online o 
        join store_returns sr on o.OrderId = sr.OrderId
        where o.Country IN ('UK','US')
        GROUP BY o.Customer) AS x
    GROUP BY o.Customer) AS t2
ON t1.Customer = t2.Customer

答案 1 :(得分:0)

此查询有效-

SELECT o.Customer, sum(o.orderPrice) as TotalValue , t2.TotalReturnValue, t2.ReturnCount
FROM orders_online o
left join(
    SELECT o.Customer, sum(o.orderPrice) as TotalReturnValue, COUNT(*) AS ReturnCount
    FROM orders_online o 
    join online_returns orr on o.OrderId = orr.OrderId
    WHERE o.Country IN ('UK', 'US')
    GROUP BY o.Customer 

    Union ALL

    SELECT o.Customer, sum(o.orderPrice) as TotalReturnValue, COUNT(*) AS ReturnCount
    FROM orders_online o 
    join store_returns sr on o.OrderId = sr.OrderId
    where o.Country IN ('UK','US')
    GROUP BY o.Customer
) as t2 ON o.Customer = t2.Customer
WHERE o.Country IN ('UK', 'US')
GROUP BY o.Customer , o.Country ,t2.TotalReturnValue,t2.ReturnCount
HAVING TotalValue > 1000

enter image description here