我有一个ids列表,一些ids可以复制。如何在一个查询中对同一个id进行求和?
SELECT SUM(`power`)
FROM `stats`
WHERE `template` IN (489191, 489191, 489100)模板489191有power =1,489100有power =0,所以我需要得到2作为结果。
发布于 2022-05-14 18:46:05
您需要使用表值数据来获得所需的行数,例如:
select sum(stats.power)
from (
select 489191 as template union all
select 489191 union all
select 489100
) as vlist
join stats on vlist.template = stats.template如果使用的是MySQL 8或更高版本,则可以使用values表值构造函数而不是union:
select sum(stats.power)
from (values
row(489191),
row(489191)
row(489100)
) as vlist(template)
join stats on vlist.template = stats.templatehttps://stackoverflow.com/questions/72242969
复制相似问题