数据库教程:MySQL两个查询如何合并成一个结果详解

mysql 查询合并如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。注

mysql 查询合并

如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。

注意:两个列表中的字段要一样才可以合并(顺序也要一样)

满足条件:

1、两个select查询的列的数量必须相同;

2、每个列的数据类型需要相似;

1.先写两条select

第一条select:

select  	date_format(add_time, '%y-%m-%d') as 'add_time',  	count(add_time) as 'reach_intention'  from  	table1  where  ## where 条件可以根据自己实际情况来定  	date_format(add_time, '%y-%m-%d') > date_format(date_sub(now(), interval 1 month), '%y-%m-%d')  group by  	date_format(add_time, '%y-%m-%d')  

这是查询出来的结果

MySQL两个查询如何合并成一个结果详解

第二条select:

select  	date_format(add_date, '%y-%m-%d') as 'add_time' ,  	count(add_date)  as 'post_release'  from  	table2  where  ## where 条件可以根据自己实际情况来定  	date_format(add_date, '%y-%m-%d') > date_format(date_sub(now(), interval 1 month), '%y-%m-%d')  group by  	date_format(add_date, '%y-%m-%d')  

第二条select查询出来的结果

MySQL两个查询如何合并成一个结果详解

2.合并查询结果

先把两条select用union all连接起来

select  		date_format(add_time, '%y-%m-%d') as 'add_time', count(add_time) as 'reach_intention' ,  '' as 'post_release'  	from  		table1  	where  		date_format(add_time, '%y-%m-%d') > date_format(date_sub(now(), interval 30 day), '%y-%m-%d')  	group by  		date_format(add_time, '%y-%m-%d')  union all  	select  		date_format(add_date, '%y-%m-%d') as 'add_time' , '' as 'reach_intention' ,  count(add_date) as 'post_release'  	from  		table2  	where  		date_format(add_date, '%y-%m-%d') > date_format(date_sub(now(), interval 30 day), '%y-%m-%d')  	group by  		date_format(add_date, '%y-%m-%d')   

在外层嵌套一个select

## 括号里面放 用 union all 连接的select   select * from () test  ## 这里要给表起个别名 不然会报 every derived table must have its own alias 每个派生表都必须有自己的别名  

把union all 连接的select 放到括号里面去然后运行

select  	*  from  (  	select  		date_format(add_time, '%y-%m-%d') as 'add_time', count(add_time) as 'reach_intention' ,  '' as 'post_release'  	from  		table1  	where  		date_format(add_time, '%y-%m-%d') > date_format(date_sub(now(), interval 30 day), '%y-%m-%d')  	group by  		date_format(add_time, '%y-%m-%d')  union all  	select  		date_format(add_date, '%y-%m-%d') as 'add_time' , '' as 'reach_intention' ,  count(add_date) as 'post_release'  	from  		table2  	where  		date_format(add_date, '%y-%m-%d') > date_format(date_sub(now(), interval 30 day), '%y-%m-%d')  	group by  		date_format(add_date, '%y-%m-%d')   ) test  

运行结果

MySQL两个查询如何合并成一个结果详解

把结果按日期分组排序。

使用 group by 和 order by 关键字

select  	add_time,  	sum(reach_intention) as 'reach_intention' ,  	sum(post_release) as 'post_release'  from  	(  	select  		date_format(add_time, '%y-%m-%d') as 'add_time', count(add_time) as 'reach_intention' , '' as 'post_release'  	from  		table1  	where  		date_format(add_time, '%y-%m-%d') > date_format(date_sub(now(), interval 30 day), '%y-%m-%d')  	group by  		date_format(add_time, '%y-%m-%d')  union all  	select  		date_format(add_date, '%y-%m-%d') as 'add_time' , '' as 'reach_intention' , count(add_date) as 'post_release'  	from  		table2  	where  		date_format(add_date, '%y-%m-%d') > date_format(date_sub(now(), interval 30 day), '%y-%m-%d')  	group by  		date_format(add_date, '%y-%m-%d') ) test  group by  	test.add_time  order by  	test.add_time desc  

结果如下

MySQL两个查询如何合并成一个结果详解

总结

到此这篇关于mysql两个查询如何合并成一个结果的文章就介绍到这了,更多相关mysql两个查询合并一个内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:MySQL两个查询如何合并成一个结果详解,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/1234124.html

(0)
上一篇 2022年8月31日
下一篇 2022年8月31日

精彩推荐