复制代码代码如下:--合并重复行select*fromAunionselect*fromB--不合并重复行select*fromAunionallselect*fromB按某个字段排序--合并重复行select*from(select*fromAunionselect*fromB)ASTorderby字段名--不合并重复行select*from(select*fromAunionallselect*fromB)ASTorderby字段名//sqlserver版Select*From(selecttop2id,adddate
--合并重复行
select * from A
union
select * from B
--不合并重复行
select * from A
union all
select * from B
按某个字段排序
--合并重复行
select *
from (
select * from A
union
select * from B) AS T
order by 字段名
--不合并重复行
select *
from (
select * from A
union all
select * from B) AS T
order by 字段名
//sql server版
Select * From (
select top 2 id,adddate,title,url from bArticle where ClassId=1 order by adddate desc) A
Union All
Select * From (
select top 2 id,adddate,title,url from bArticle where ClassId=2 order by adddate desc) B
Union All
Select * From (
select top 2 id,adddate,title,url from bArticle where ClassId=3 order by adddate desc) C
Union All
Select * From (
select top 2 id,adddate,title,url from bArticle where ClassId=4 order by adddate desc) D
//mysql版
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 All
Select * From (
select id,adddate,title,url from bArticle where ClassId=4 order by adddate desc limit 0,2) D
标签: sql 查询 结果 合并 unionall 用法 数据库 技巧
声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!