将两条sql语句合并成一条

select date_format(time,'%Y-%m-%d') as title, count(*) as num from meter where 1=1和select count(*) as imm_num from user u inner join meter m on u.id=m.userId and date_format(u.time,'%Y-%m-%d')=date_format(m.time,'%Y-%m-%d')能合并吗 求大神指点

sql

1个回答

你这种情况属于列集合并,实现如下:
SELECT a.title,a.num,b.imm_num FROM
(select date_format(time,'%Y-%m-%d') as title, count(*) as num from meter where 1=1)a,
(select count(*) as imm_num from user u inner join meter m on u.id=m.userId and date_format(u.time,'%Y-%m-%d')=date_format(m.time,'%Y-%m-%d'))b
若是列集字段一样,涉及到记录集合并
只需在两个SELECT中加UNION ALL即可

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问