时间:2021-07-01 10:21:17 帮助过:1人阅读
使用union 或union all
如select * from a union all select * from b 竖排合并两个表的数据
显示格式如:
a date
-204 2015-07-06
220 2015-07-15
-60 2015-07-16
220 2015-07-17
//另外一种情况,根据不同条件得到的值赋予不一样的别名(在不同的列显示)
SELECT a.date, sum(a.c1) AS s1, SUM(a.c2) AS s2 FROM( SELECT DATE_FORMAT(b.time, ‘%Y-%m-%d‘) AS date, IFNULL(( CASE b.sta WHEN 0 THEN COUNT(*) END),0 ) AS c1, IFNULL(( CASE b.sta WHEN 1 THEN COUNT(*) END),0 ) AS c2 FROM b GROUP BY b.sta,date ) AS a GROUP BY a.date
//根据类型sta=0/=1,时间分组,统计个数
得到的数据格式:
date s1 s2
2015-07-06 4 1
2015-07-08 0 1
2015-07-15 0 1
2015-07-16 1 3
sql 合并查询结果
标签: