SQL 数个表中相同ID的求和查询
有数个表(表名以日期命名):***********、***********、***********、***********……每个表里内容:ID、销售额、日期……用SQL查询 某个时间段 某单品的销售和,如:求ID为123在***********至***********期间的销售总和。怎么写呢?注:期间某天此商品可能无销售,那么那天的表里就无此商品的ID。如:***********那天123无销售,表***********里ID就没有123
参考答案:求ID为123在***********至***********期间的销售总和:
select id,sum(销售额)
from (
select *from ***********
Union
select *from ***********
Union
select *from ***********
Union
select *from ***********
)
where id=123
group by id
或
select id,sum(销售额)
from (
select *from *********** where id=123
Union
select *from *********** where id=123
Union
select *from *********** where id=123
Union
select *from *********** where id=123
)
group by id