当前位置:网站首页>Oracle 组合查询
Oracle 组合查询
2022-07-27 06:10:00 【zhiyou-rookie】
Oracle 组合查询
大多数SQL查询都包含单独一条SELECT语句,用于从一个或多个表中返回数据。Oracle还允许执行多个多个查询(多条SELECT语句),并返回结果作为单个查询结果集。这些组合查询通常被称为联合或复合查询。
使用组合查询的两种情况:
- 要通过单个查询从不同表中返回类似结构化的数据
- 要对单个表执行多个查询,并作为一个查询返回数据。
提示:组合查询与多个where条件
一般而言,组合两个对同一个表的查询可以完成与具有多个where子句条件的单个查询所做的相同事情。换句话说,也可以把具有多个where子句的任何select语句指定为一个组合查询。
1、创建组合查询
可以使用UNION运算符组合SQL查询。使用UNION可以指定多条SELECT语句,并且可以把它们的结果组合成单个结果集。
1.1 使用union
union的用法很简单,只需指定每一条select语句,并在它们之间放置关键字UNION。
下面看一条使用示例:
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
UNION
SELECT vend_id, prod_id, prod_price
FROM products
WHERE vend_id IN (1001,1002);
以上语句使用where子句替换“:
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
OR vend_id IN (1001,1002);
1.2 UNION规则
以下列出一些UNION规则:
- union必须由两个或更多的select语句组成,每条语句都都用关键字union分隔开(因此,如果组合4条select语句,则要使用3个UNION关键字)。
- UNION中的每个查询都必须包含相同的列、表达式或聚合函数(尽管列不必以相同的顺序列出)。
- 列数据类型必须是兼容的。它们不必是完全相同的类型,但是它们必须是Oracle可以隐式转换的类型(例如:不同的数值类型或者不同的日期类型)。
1.3 包括或消除重复的行
union将从查询结果集中自动删除任何重复的行(换句话说,它就像单条SELECT语句中的多个WHERE子句条件一样)。但是如果需要,也可以改变他。事实上,如果确实希望返回出现的所有匹配,可以使用UNION ALL代替UNION。
UNION ALL使用示例:
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
UNION ALL
SELECT vend_id, prod_id, prod_price
FROM products
WHERE vend_id IN (1001,1002);
提示:
UNION几乎可以完成与多个where条件相同的事情。union all是union的一种形式,它可以完成不能利用where子句完成的事情。事实上,如果确实想要返回对于每个条件而出现的所有匹配(包括重复的行),就必须使用union all,而不使用where。
1.4 对组合查询的结果进行排序
SELECT语句的输出是使用ORDER BY子句进行排序的。当利用UNION组合查询时,只能是使用一个ORDER BY子句,并且它必须出现最后一个SELECT语句之后。
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
UNION
SELECT vend_id, prod_id, prod_price
FROM products
WHERE vend_id IN (1001,1002)
ORDER BY vend_id, prod_price;
这个UNION在第二个SELECT语句之后带有单个ORDER BY子句。即使ORDER BY看似只是最后一个SELECT语句的一部分,Oracle事实上也将对由所有SELECT语句返回的所有结果进行排序。
边栏推荐
- How to learn C language? This article gives you the complete answer
- Brief introduction of simulation model
- Common problems in converting pytorch to onnx
- MySQL2
- Synchronized锁
- Jest single test style problem [identity obj proxy] NPM package
- Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
- Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
- Campus news release management system based on SSM
- (posted) comparison of Eureka, consumer and Nacos 2
猜你喜欢

How to implement Devops with automation tools | including low code and Devops application practice

Internal class -- just read this article~

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces

零号培训平台课程-2、SSRF基础

Which C4d cloud rendering platform to cooperate with?

How to learn C language? This article gives you the complete answer

整体二分?

Music website management system based on SSM

Automatically generate UML sequence diagram according to text (draw.io format)

Summary of APP launch in vivo application market
随机推荐
No.0 training platform course-2. SSRF Foundation
Digital image processing Chapter 1 Introduction
R2live code learning record (3): radar feature extraction
[latex format] there are subtitles side by side on the left and right of double columns and double pictures, and subtitles are side by side up and down
线性表 -- 栈和队列
Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
Gbase 8C product introduction
Livox Slam (with lio+ closed loop detection optimization)
VIM editor deletes all file contents
py2exe qt界面风格变成了win98解决方案
Reflection on pytorch back propagation
火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。
ADB instruction sorting
李沐动手学深度学习V2-transformer和代码实现
Quartus: an error is reported when adding a.V file to someone else's project
(posted) comparison of Eureka, consumer and Nacos 1
Vscode creates golang development environment and debug unit test of golang
Oracle数据库问题
如何取得对象的DDL信息
MySQL2