对 sqlite3 中具有相同 id 的所有行求和
Sum all rows that have the same id in sqlite3
我正在使用 sqlite3,我有以下 table:
"who"代表用户id。说 ID 为“4”的人代表“John”。约翰进行了 7 次购买。我想要一个 select 查询来汇总 John 的所有股份。所以 select 查询基于“4”returns 113 中谁的股票。我该怎么做?
你想要一个 where
子句和一个 sum()
:
select sum(shares) as total_shares
from mytable
where who = 4
或者,如果您希望一次获得所有用户的结果,每个用户在单独的行中:
select who, sum(shares) as total_shares
from mytable
group by who
如果您愿意,您还可以统计每个用户有多少条记录:
select who, sum(shares) as total_shares, count(*) as cnt
from mytable
group by who
我正在使用 sqlite3,我有以下 table:
"who"代表用户id。说 ID 为“4”的人代表“John”。约翰进行了 7 次购买。我想要一个 select 查询来汇总 John 的所有股份。所以 select 查询基于“4”returns 113 中谁的股票。我该怎么做?
你想要一个 where
子句和一个 sum()
:
select sum(shares) as total_shares
from mytable
where who = 4
或者,如果您希望一次获得所有用户的结果,每个用户在单独的行中:
select who, sum(shares) as total_shares
from mytable
group by who
如果您愿意,您还可以统计每个用户有多少条记录:
select who, sum(shares) as total_shares, count(*) as cnt
from mytable
group by who