当前位置: 网站首页 > 热门专题 > 数据库恢复专题 >

sql语句查询结果合并union all用法

时间:2010-08-23 23:19  来源:   点 击:
导读』:MsSQL:sql语句查询结果合并union all用法,Mssql基础教程软件学院..
关键字:MsSQL,Mssql基础教程

整理别人的sql

大概的思想是用union 和union all

--合并重复行
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
http://blog.csdn.net/kemy88/archive/2007/02/28/1516913.aspx

很强大!
(0)
0%
烂透了!
(0)
0%
编辑:

分享与收藏:
您可能感兴趣的文章

数据恢复

热门技术文章

硬盘常见问题