Mysql 非常pid,计算t1q、t2q、t3q,其中t1q是表1时记录数量的总和。to_id=10,t2q是表1_2时记录数量的总和。from_id=10,t3q是表3时记录数量的总和。某些_id=10。我们如何才能做到这一点?如果这是不可能的,我是否应该使用cr
Mysql 非常pid,计算t1q、t2q、t3q,其中t1q是表1时记录数量的总和。to_id=10,t2q是表1_2时记录数量的总和。from_id=10,t3q是表3时记录数量的总和。某些_id=10。我们如何才能做到这一点?如果这是不可能的,我是否应该使用cr,mysql,sql,join,aggregate,Mysql,Sql,Join,Aggregate,非常pid,计算t1q、t2q、t3q,其中t1q是表1时记录数量的总和。to_id=10,t2q是表1_2时记录数量的总和。from_id=10,t3q是表3时记录数量的总和。某些_id=10。我们如何才能做到这一点?如果这是不可能的,我是否应该使用cron来准备中间表,其中包含使用forloops计算的数据。对不起,我没用。根据这个例子,是否有一种使用“case”的方法?似乎另一个答案是有效的。无论如何,谢谢你的帮助。也许你的答案是对的,但对我来说,产品id重复了很多次。哦,那么你说我的解决
非常pid,计算t1q、t2q、t3q,其中t1q是表1时记录数量的总和。to_id=10,t2q是表1_2时记录数量的总和。from_id=10,t3q是表3时记录数量的总和。某些_id=10。我们如何才能做到这一点?如果这是不可能的,我是否应该使用cron来准备中间表,其中包含使用forloops计算的数据。对不起,我没用。根据这个例子,是否有一种使用“case”的方法?似乎另一个答案是有效的。无论如何,谢谢你的帮助。也许你的答案是对的,但对我来说,产品id重复了很多次。哦,那么你说我的解决方案在没有实际尝试的情况下“不起作用”?那很酷。。。case-when的另一个解决方案也可以,但执行速度较慢,而且比我的解决方案复杂得多。但不管怎样。使用SQL解决问题总是有多种方法。我相信我的解决方案对你的问题来说是最优雅的。我在你回答后立即尝试了。当我尝试时,我得到了相同的pid重复多次。无论如何,我会再试一次。再给你一个。谢谢。对不起,我没有工作。根据这个例子,是否有一种使用“case”的方法?似乎另一个答案是有效的。无论如何,谢谢你的帮助。也许你的答案是对的,但对我来说,产品id重复了很多次。哦,那么你说我的解决方案在没有实际尝试的情况下“不起作用”?那很酷。。。case-when的另一个解决方案也可以,但执行速度较慢,而且比我的解决方案复杂得多。但不管怎样。使用SQL解决问题总是有多种方法。我相信我的解决方案对你的问题来说是最优雅的。我在你回答后立即尝试了。当我尝试时,我得到了相同的pid重复多次。无论如何,我会再试一次。再给你一个。谢谢。你的第二个答案很有效,谢谢。但当t1q、t2q或t3q中的最后一个为0时,它将忽略pids。我们能在1、0、0或5、0、2或0、5、1等情况下获得输出吗?您的第二个答案有效,谢谢。但当t1q、t2q或t3q中的最后一个为0时,它将忽略pids。我们能在像1,0,0或5,0,2或0,5,1这样的情况下得到输出吗?
select
sum(table1.quantity) as t1q,
sum(table1_2.quantity) as t2q,
sum(table3.quantity) as t3q,
table1.pid as pid
from table1
inner join table3 on table1.pid=table3.pid
inner join table1 table1_2 on table1.pid=table1_2.pid
where
table1.to_id=10 and
table3.some_id=10 and
table1_2.from_id=10
group by pid;
Table1:
quantity, to_id, from_id, pid
6, 10, 999999, 345
4, 888999, 10, 345
3, 888999, 10, 345
Table1 (same table used twice as table1 and table1_2)
Table3
SELECT a.pid,
b.t1q,
c.t2q,
d.t3q
FROM (SELECT DISTINCT(pid) FROM table1 AS pid) a
JOIN (SELECT pid, SUM(quantity) AS t1q FROM table1 WHERE to_id = 10 GROUP BY pid) b ON a.pid = b.pid
JOIN (SELECT pid, SUM(quantity) AS t2q FROM table1 WHERE from_id = 10 GROUP BY pid) c ON a.pid = b.pid
JOIN (SELECT pid, SUM(quantity) AS t3q FROM table3 WHERE some_id = 10 GROUP BY pid) d ON a.pid = d.pid
select
sum(table1.quantity) as t1q,
sum(table1_2.quantity) as t2q,
sum(table3.quantity) as t3q,
table1.pid as pid
from
table1
inner join
table3
on table1.pid=table3.pid
inner join
(
SELECT
pid,
SUM(quantity) AS quantity
FROM
table1
WHERE
from_id = 10
GROUP BY
pid
)
table1_2
on table1.pid=table1_2.pid
where
table1.to_id=10 and
table3.some_id=10
group by
pid
select
table1.quantity as t1q,
table1_2.quantity as t2q,
table3.quantity as t3q,
table1.pid as pid
from
(
SELECT
pid,
SUM(quantity) AS quantity
FROM
table1
WHERE
to_id = 10
GROUP BY
pid
)
table1
inner join
(
SELECT
pid,
SUM(quantity) AS quantity
FROM
table3
WHERE
some_id = 10
GROUP BY
pid
)
table3
on table1.pid=table3.pid
inner join
(
SELECT
pid,
SUM(quantity) AS quantity
FROM
table1
WHERE
from_id = 10
GROUP BY
pid
)
table1_2
on table1.pid=table1_2.pid
SELECT
COALESCE(table1.t1q, 0) AS t1q,
COALESCE(table1.t2q, 0) AS t2q,
COALESCE(table3.t3q, 0) AS t3q,
COALESCE(table1.pid, table3.pid) AS pid
FROM
(
SELECT
pid,
SUM(CASE WHEN to_id = 10 THEN quantity ELSE 0 END) AS t1q,
SUM(CASE WHEN from_id = 10 THEN quantity ELSE 0 END) AS t2q
FROM
table1
WHERE
to_id = 10
OR from_id = 10
GROUP BY
pid
)
table1
FULL OUTER JOIN
(
SELECT
pid,
SUM(quantity) AS quantity
FROM
table3
WHERE
some_id = 10
GROUP BY
pid
)
table3
ON table1.pid = table3.pid
SELECT
SUM(t1q) AS t1q,
SUM(t2q) AS t2q,
SUM(t3q) AS t3q,
pid
FROM
(
SELECT pid, quantity AS t1q, 0 AS t2q, 0 AS t3q FROM table1 WHERE to_id = 10
UNION ALL
SELECT pid, 0 , quantity, 0 FROM table1 WHERE from_id = 10
UNION ALL
SELECT pid, 0 , 0 , quantity FROM table3 WHERE some_id = 10
)
combined
GROUP BY
pid