嗨,我有一张桌子如下:
用户表
id val1 val2 val3
1 2 3 2
2 0 0 1
我想知道如何从 id = 1 的列 val1,val2,val3 中计算值?这意味着id = 1
总计为 7,id = 2
总计为 1。大多数 SO 示例计算所有 id 的整个列。我的解决方案如下似乎没有得到首选结果
select count(*) as tot
from (
select val1 as total from user
where id=1
union
select val2 as total from user
where id=1
union
select val3 as total from user
where id=1
) as x
感谢帮助。