当前位置: 代码迷 >> MySQL >> MySQL多结果集归拢union查询
  详细解决方案

MySQL多结果集归拢union查询

热度:309   发布时间:2016-05-05 16:21:55.0
MySQL多结果集合并union查询
需求:
查询规则自定义,由数据库保存,返回结果字段来自同一张表;
要实现多个规则查询条件符合的适配,最终将查询结果合并展示出来,并标注是哪个规则的结果;

Mysql例子
--合并重复行  select * from A  union   select * from B      --不合并重复行  select * from A  union all  select * from B  Select * From (select id,adddate,title,url from bArticle where ClassId=1 order by adddate desc limit 0,2) A    Union All  Select * From (select id,adddate,title,url from bArticle where ClassId=2 order by adddate desc limit 0,2) B  Union All  Select * From (select id,adddate,title,url from bArticle where ClassId=3 order by adddate desc limit 0,2) C  Union AllSelect * From (select id,adddate,title,url from bArticle where ClassId=4 order by adddate desc limit 0,2) D  


实践:
这里使用的是mysql默认的数据库mysql



SELECT * FROM (SELECT *,(SELECT 'A' as A) tag FROM help_topic) A UNION ALLSELECT * FROM (SELECT *,(SELECT 'B' as A) tag FROM help_topic) B UNION ALLSELECT * FROM (SELECT *,(SELECT 'C' as A) tag FROM help_topic) C UNION ALLSELECT * FROM (SELECT *,(SELECT 'D' as A) tag FROM help_topic) D

效果图:


  相关解决方案