こんにちは、私は次のようなテーブルを持っています:
ユーザーテーブル
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
手伝ってくれてありがとう。