UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果
例如:
SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales
注意:union用法中,两个select语句的字段类型匹配,而且字段个数要相同,如上面的例子,在实际的软件开发过程,会遇到更复杂的情况,具体请看下面的例子
select '1' as type,FL_ID,FL_CODE,FL_CNAME,FLDAFL_PARENTID from FLDA
WHERE ZT_ID=2006030002
union
select '2' as type,XM_ID,XM_CODE ,XM_CNAME ,FL_ID from XMDA
where exists (select from (select FL_ID from FLDA WHERE ZT_ID=2006030002 ) a where XMDAfl_id=afl_id)
order by type,FL_PARENTID ,FL_ID
这个句子的意思是将两个sql语句union查询出来,查询的条件就是看XMDA表中的FL_ID是否和主表FLDA里的FL_ID值相匹配,(也就是存在)
UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。
在查询中会遇到 UNION ALL,它的用法和union一样,只不过union含有distinct的功能,它会把两张表了重复的记录去掉,而union all不会,所以从效率上,union all 会高一点,但在实际中用到的并不是很多
表头会用第一个连接块的字段。。。。。。。。。。
而UNION ALL只是简单的将两个结果合并后就返回。这样,如果返回的两个结果集中有重复的数据,那么返回的结果集就会包含重复的数据了。
从效率上说,UNION ALL 要比UNION快很多,所以,如果可以确认合并的两个结果集中不包含重复的数据的话,那么就使用UNION ALL,如下:
尽量使用union all,因为union需要进行排序,去除重复记录,效率低
>
你可以在这个链接里看到相关的内容
给你个通用分页存储过程吧:
CREATE PROCEDURE SupesoftPage
(
@TableName nvarchar(50), -- 表名
@ReturnFields nvarchar(2000) = '', -- 需要返回的列
@PageSize int = 10, -- 每页记录数
@PageIndex int = 1, -- 当前页码
@Where nvarchar(2000) = '', -- 查询条件
@Orderfld nvarchar(2000), -- 排序字段名 最好为唯一主键
@OrderType int = 1 -- 排序类型 1:降序 其它为升序
)
AS
DECLARE @TotalRecord int
DECLARE @TotalPage int
DECLARE @CurrentPageSize int
DECLARE @TotalRecordForPageIndex int
DECLARE @OrderBy nvarchar(255)
DECLARE @CutOrderBy nvarchar(255)
if @OrderType = 1
BEGIN
set @OrderBy = ' Order by ' + REPLACE(@Orderfld,',',' desc,') + ' desc '
set @CutOrderBy = ' Order by '+ REPLACE(@Orderfld,',',' asc,') + ' asc '
END
else
BEGIN
set @OrderBy = ' Order by ' + REPLACE(@Orderfld,',',' asc,') + ' asc '
set @CutOrderBy = ' Order by '+ REPLACE(@Orderfld,',',' desc,') + ' desc '
END
-- 记录总数
declare @countSql nvarchar(4000)
set @countSql='SELECT @TotalRecord=Count() From '+@TableName+' '+@Where
execute sp_executesql @countSql,N'@TotalRecord int out',@TotalRecord out
SET @TotalPage=(@TotalRecord-1)/@PageSize+1
SET @CurrentPageSize=@PageSize
IF(@TotalPage=@PageIndex)
BEGIN
SET @CurrentPageSize=@TotalRecord%@PageSize
IF(@CurrentPageSize=0)
SET @CurrentPageSize=@PageSize
END
-- 返回记录
set @TotalRecordForPageIndex=@PageIndex@PageSize
exec('SELECT FROM
(SELECT TOP '+@CurrentPageSize+' FROM
(SELECT TOP '+@TotalRecordForPageIndex+' '+@ReturnFields+'
FROM '+@TableName+' '+@Where+' '+@OrderBy+') TB2
'+@CutOrderBy+') TB3
'+@OrderBy)
-- 返回总页数和总记录数
SELECT @TotalPage as PageCount,@TotalRecord as RecordCount
GO
-- 少了一个括号
select id,bz,yfrq from (
select 't112_'+cast(dt1id as varchar(500)) as id,bz,yfrq from formtable_main_112_dt1 dt1 left join formtable_main_112 ht1 on (ht1id = dt1mainid)
union all
select 't113_'+cast(dt2id as varchar(500)) as id,dt2bz,yfrq from formtable_main_113_dt1 dt2 left join formtable_main_113 ht2 on (ht2id = dt2mainid)
union all
select 't114_'+cast(dt3id as varchar(500)) as id,fkxm as bz,yfrq from formtable_main_114_dt1 dt3 left join formtable_main_114 ht3 on (ht3id = dt3mainid)
)
你的需求并不是一般所说的多表查询,一般讲多表查询通常是指多表连接查询,或者是嵌套查询。其实你的需求只要将各个查询结果进行联合就可以了。
select
from
A
where
name
like
'%a%'
union
all
select
from
B
where
name
like
'%a%'
union
all
select
from
C
where
name
like
'%a%'
union
all
select
from
D
where
name
like
'%a%'
以上就是关于oracle 里SQL语句UNION怎么用全部的内容,包括:oracle 里SQL语句UNION怎么用、sql 中union查询获取部分结果、sql server 中的union 联合查询问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)