sql 查询结果合并union all用法_数据库技巧
1个回答
展开全部
复制代码
代码如下:
--合并重复行
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
代码如下:
--合并重复行
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询