我有 2 个表,想要从 2 个有条件的表中获取行总和 结果是这样的,例如 sh2 : 19 + 5 + 11 + 5 = 40 对于每个名称将它们全部计算在表中 我使用该代码
select name, sum(total) total from
(select name, sum(coalesce(sk,ss)) total
from table1
group by name
union all
select name, sum(coalesce(sk,ss)) total
from table2
group by name
) t
group by name
但我想对该代码使用 select 和 case
最佳答案
select name,SUM(total)total from(
select name,sum(case when sk>0 then sk else ss end )total from majale group by name
union all
select name,sum(case when sk>0 then sk else ss end )from ketab group by name
union all
select name,sum(case when sk>0 then sk else ss end )from savabegh group by name
)t
group by name
关于mysql - sql合并成格以选择案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43202691/