我有2张桌子
table1,它有列
id
sdate
type
类型列可以有2个值事件或计划我正在使用此查询来获取总事件和计划的结果
select date_format(sdate, '%m-%d-%Y') as sdate, sum( type ='event') as tevent , sum(type='schedule') as tschedule from table1 where sid ='1' group by (sdate);
表2包含这些列
id
title
dtime
使它们两者类似于在联合中使用我做了类似的事情
select date_format(dtime, '%m-%d-%Y') as sdate ,0 as tevent,0 as tschedule,count(id) as tlog from table2 where sid =1 group by (sdate) ;
我有点困惑,我怎么能从这两个表中获取数据,如果日期相同,它应该在一列中显示数据。
答案 0 :(得分:1)
试试这个(包括对原始查询的一些更正):
select sdate, sum(tevent) as tevent,
sum(tschedule) as tschedule, sum(tlog) as tlog
from (
select date_format(sdate, '%m-%d-%Y') as sdate,
sum(type='event') as tevent ,
sum(type='schedule') as tschedule,
0 as tlog from table1
group by sdate
union
select date_format(dtime, '%m-%d-%Y') as sdate ,
0 as tevent,0 as tschedule,
count(id) as tlog from table2
group by sdate
) s group by sdate;