想要从 2 个不同的表中获取 2 个不同列的总和并想要显示最终总和
Want to get sum of 2 different columns from 2 different tables and want to display final sum
SELECT
SUM(p.price + c.price) AS product_weekly_total
FROM t_product_purchase p
LEFT JOIN
t_coupon_purchase c on c.storepkid=p.storepkid ";
WHERE
(p.purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59') OR
(c.purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59')
GROUP BY
p.storepkid
ORDER BY
product_weekly_total DESC LIMIT 0,5
我想获得 price 和 cprice 字段之和的前 5 个最高总记录。但是上面的查询给了我错误的结果。
如果我从上面的查询中删除 't_coupon_purchase' table 并仅使用 't_product_purchase' 触发查询,那么它会给我正确的结果和正确的数量。
请让我知道我在查询中做错了什么,或者我应该用其他我不知道的方式来写它。请提供一些帮助。
---- 编辑 - 2015 年 1 月 1 日 ----
SQL fiddle 我的查询
http://www.sqlfiddle.com/#!2/0d4523/8
这里我提到了每个storeid的总数,这样有助于与查询结果进行比较。
5 = 67 + 68 + 115 = 250
1 = 57 + 50 + 75 + 66 = 248
3 = 70 + 144 = 214
2 = 23 + 100 + 22 + 27 = 172
4 = 15 + 7 + 105 = 127
9 = 34
7 = 33
首先,我认为您的第 5 行有错字 t_coupon_purchase c on c.storeid=p.storeid ";
。 ";
不应该在那里。
此外,您对日期的限制应该只有一个 OR
。该代码未执行,因为在您的第二个 OR
之后,没有其他约束。
由于您是 LEFT JOINing,因此您可能会添加一个带有来自 LEFT JOINed table 的不匹配 NULL 的数字。另外,我认为您不能像在查询中那样对结果进行排序。您可以尝试以下操作,看看会得到什么吗?
select storeid, product_weekly_total from
(
SELECT
p.storeid, SUM(coalesce(p.price,0) + coalesce(c.cprice,0)) AS product_weekly_total
FROM t_product_purchase p
LEFT JOIN
t_coupon_purchase c on c.storeid=p.storeid
WHERE
(p.purchasedate BETWEEN '2014-12-27' AND '2015-01-03') OR
(c.cpurchasedate BETWEEN '2014-12-27' AND '2015-01-03')
GROUP BY
p.storeid
) as X
ORDER BY
product_weekly_total DESC LIMIT 0,5
所以你实际上是在排序总和的最终结果。
谢谢
编辑
从您在 fiddle 中看到的数据模型,行:
(11, 7, 33.00, '2015-12-01 01:01:01'),
(12, 1, 75.00, '2015-12-01 07:07:57'),
(13, 5, 68.00, '2015-12-01 22:22:00'),
(14, 9, 34.00, '2015-12-01 09:06:00'),
(15, 2, 27.00, '2015-12-01 17:06:00');
不会返回,因为日期是 2015 年 12 月 1 日。
只加入您想加入的人。在这里,您想加入本周的产品购买和本周的优惠券购买。但是你加入的是任何产品购买和任何优惠券购买(其中至少一对夫妇必须在给定的一周内购买)。因此,请汇总您的产品购买和优惠券购买,然后加入。
select
coalesce(p.storeid,c.storeid) as storeid,
coalesce(p.price,0) + coalesce(c.price,0) as product_weekly_total
from
(
select storeid, sum(price) as price
from t_product_purchase
where purchasedate between '2014-12-27' and '2015-01-03'
group by storeid
) p
full outer join
(
select storeid, sum(cprice) as price
from t_coupon_purchase
where cpurchasedate between '2014-12-27' and '2015-01-03'
group by storeid
) c on c.storeid = p.storeid
order by product_weekly_total desc
limit 5;
我在此处使用完全外部联接,以便仅在其中一张表中获取本周有采购的商店。
编辑:我犯了两个错误。一件事是我做了 p.price + c.price
,但是加数可以是 NULL,所以它必须是 coalesce(p.price,0) + coalesce(c.price,0)
。我已经在上面的查询中更改了它。另一件事是 MySQL 不支持完全外部联接。我没有在上面的查询中更改它,以保持查询的可读性。以下是如何更改它以模仿 MySQL 中的完整外部联接:A full outer join B
是 A left outer join B union A right outer join B
。所以:
select storepkid, product_weekly_total
from
(
select
coalesce(p.storepkid,c.storepkid) as storepkid,
coalesce(p.price,0) + coalesce(c.price,0) as product_weekly_total
from
(
select storepkid, sum(price) as price
from t_product_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) p
left outer join
(
select storepkid, sum(price) as price
from t_coupon_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) c on c.storepkid = p.storepkid
union
select
coalesce(p.storepkid,c.storepkid) as storepkid,
coalesce(p.price,0) + coalesce(c.price,0) as product_weekly_total
from
(
select storepkid, sum(price) as price
from t_product_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) p
right outer join
(
select storepkid, sum(price) as price
from t_coupon_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) c on c.storepkid = p.storepkid
) all_results
order by product_weekly_total desc
limit 5;
以下查询适用于我。
SELECT t.storepkid, SUM(t.price) AS product_weekly_total
FROM
(
SELECT storepkid AS storepkid, price AS price
FROM t_product_purchase
WHERE purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
UNION ALL
SELECT storepkid, price FROM t_coupon_purchase
WHERE purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
) t
GROUP BY storepkid
ORDER BY product_weekly_total DESC LIMIT 0,5
SQL fiddle 可在 link 获得:http://www.sqlfiddle.com/#!2/f269b/46
SELECT
SUM(p.price + c.price) AS product_weekly_total
FROM t_product_purchase p
LEFT JOIN
t_coupon_purchase c on c.storepkid=p.storepkid ";
WHERE
(p.purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59') OR
(c.purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59')
GROUP BY
p.storepkid
ORDER BY
product_weekly_total DESC LIMIT 0,5
我想获得 price 和 cprice 字段之和的前 5 个最高总记录。但是上面的查询给了我错误的结果。
如果我从上面的查询中删除 't_coupon_purchase' table 并仅使用 't_product_purchase' 触发查询,那么它会给我正确的结果和正确的数量。
请让我知道我在查询中做错了什么,或者我应该用其他我不知道的方式来写它。请提供一些帮助。
---- 编辑 - 2015 年 1 月 1 日 ---- SQL fiddle 我的查询 http://www.sqlfiddle.com/#!2/0d4523/8
这里我提到了每个storeid的总数,这样有助于与查询结果进行比较。
5 = 67 + 68 + 115 = 250
1 = 57 + 50 + 75 + 66 = 248
3 = 70 + 144 = 214
2 = 23 + 100 + 22 + 27 = 172
4 = 15 + 7 + 105 = 127
9 = 34
7 = 33
首先,我认为您的第 5 行有错字 t_coupon_purchase c on c.storeid=p.storeid ";
。 ";
不应该在那里。
此外,您对日期的限制应该只有一个 OR
。该代码未执行,因为在您的第二个 OR
之后,没有其他约束。
由于您是 LEFT JOINing,因此您可能会添加一个带有来自 LEFT JOINed table 的不匹配 NULL 的数字。另外,我认为您不能像在查询中那样对结果进行排序。您可以尝试以下操作,看看会得到什么吗?
select storeid, product_weekly_total from
(
SELECT
p.storeid, SUM(coalesce(p.price,0) + coalesce(c.cprice,0)) AS product_weekly_total
FROM t_product_purchase p
LEFT JOIN
t_coupon_purchase c on c.storeid=p.storeid
WHERE
(p.purchasedate BETWEEN '2014-12-27' AND '2015-01-03') OR
(c.cpurchasedate BETWEEN '2014-12-27' AND '2015-01-03')
GROUP BY
p.storeid
) as X
ORDER BY
product_weekly_total DESC LIMIT 0,5
所以你实际上是在排序总和的最终结果。
谢谢
编辑 从您在 fiddle 中看到的数据模型,行:
(11, 7, 33.00, '2015-12-01 01:01:01'),
(12, 1, 75.00, '2015-12-01 07:07:57'),
(13, 5, 68.00, '2015-12-01 22:22:00'),
(14, 9, 34.00, '2015-12-01 09:06:00'),
(15, 2, 27.00, '2015-12-01 17:06:00');
不会返回,因为日期是 2015 年 12 月 1 日。
只加入您想加入的人。在这里,您想加入本周的产品购买和本周的优惠券购买。但是你加入的是任何产品购买和任何优惠券购买(其中至少一对夫妇必须在给定的一周内购买)。因此,请汇总您的产品购买和优惠券购买,然后加入。
select
coalesce(p.storeid,c.storeid) as storeid,
coalesce(p.price,0) + coalesce(c.price,0) as product_weekly_total
from
(
select storeid, sum(price) as price
from t_product_purchase
where purchasedate between '2014-12-27' and '2015-01-03'
group by storeid
) p
full outer join
(
select storeid, sum(cprice) as price
from t_coupon_purchase
where cpurchasedate between '2014-12-27' and '2015-01-03'
group by storeid
) c on c.storeid = p.storeid
order by product_weekly_total desc
limit 5;
我在此处使用完全外部联接,以便仅在其中一张表中获取本周有采购的商店。
编辑:我犯了两个错误。一件事是我做了 p.price + c.price
,但是加数可以是 NULL,所以它必须是 coalesce(p.price,0) + coalesce(c.price,0)
。我已经在上面的查询中更改了它。另一件事是 MySQL 不支持完全外部联接。我没有在上面的查询中更改它,以保持查询的可读性。以下是如何更改它以模仿 MySQL 中的完整外部联接:A full outer join B
是 A left outer join B union A right outer join B
。所以:
select storepkid, product_weekly_total
from
(
select
coalesce(p.storepkid,c.storepkid) as storepkid,
coalesce(p.price,0) + coalesce(c.price,0) as product_weekly_total
from
(
select storepkid, sum(price) as price
from t_product_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) p
left outer join
(
select storepkid, sum(price) as price
from t_coupon_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) c on c.storepkid = p.storepkid
union
select
coalesce(p.storepkid,c.storepkid) as storepkid,
coalesce(p.price,0) + coalesce(c.price,0) as product_weekly_total
from
(
select storepkid, sum(price) as price
from t_product_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) p
right outer join
(
select storepkid, sum(price) as price
from t_coupon_purchase
where purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
group by storepkid
) c on c.storepkid = p.storepkid
) all_results
order by product_weekly_total desc
limit 5;
以下查询适用于我。
SELECT t.storepkid, SUM(t.price) AS product_weekly_total
FROM
(
SELECT storepkid AS storepkid, price AS price
FROM t_product_purchase
WHERE purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
UNION ALL
SELECT storepkid, price FROM t_coupon_purchase
WHERE purchasedatetime BETWEEN '2014-12-27 00:00:01' AND '2015-01-02 23:59:59'
) t
GROUP BY storepkid
ORDER BY product_weekly_total DESC LIMIT 0,5
SQL fiddle 可在 link 获得:http://www.sqlfiddle.com/#!2/f269b/46