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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 查询结果合并union all用法_数据库技巧相关的知识,希望对你有一定的参考价值。

参考技术A 复制代码
代码如下:
--合并重复行
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 all用法_数据库技巧的主要内容,如果未能解决你的问题,请参考以下文章

mysql中的union用法

sql中用union all最多可以合并几个表

请教:SQL同一数据库中,两个查询结果数据类型不同时的union all 合并问题

SQL 占位符 和 union all

SQL 占位符 和 union all

union和union all的区别