当前位置:Gxlcms > mssql > sql查询结果合并unionall用法_数据库技巧

sql查询结果合并unionall用法_数据库技巧

时间:2021-07-01 10:21:17 帮助过:62人阅读

代码如下:

--合并重复行
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 Union和Union All的使用方法
  • sqlserver Union和SQL Union All使用方法
  • Sql学习第一天——SQL UNION 和 UNION ALL 操作符认识
  • Mysql联合查询UNION和UNION ALL的使用介绍
  • MySQL中使用or、in与union all在查询命令下的效率对比
  • SQL语句之Union和Union All的用法

人气教程排行