当前位置:网站首页>Oracle composite query
Oracle composite query
2022-07-27 07:29:00 【zhiyou-rookie】
Oracle Combination query
majority SQL Each query contains a single SELECT sentence , Used to return data from one or more tables .Oracle It also allows multiple queries to be executed ( multiple SELECT sentence ), And return the result as a single query result set . These combined queries are often referred to as joint or compound queries .
Two cases of using combined queries :
- To return similar structured data from different tables through a single query
- To perform multiple queries on a single table , And return data as a query .
Tips : Combine queries with multiple where Conditions
generally speaking , Combining two queries on the same table can be accomplished with multiple where A single query with Clause conditions does the same thing . let me put it another way , You can also have multiple where Any of clauses select Statement is specified as a combined query .
1、 Create a composite query
have access to UNION Operator combinations SQL Inquire about . Use UNION You can specify more than one SELECT sentence , And their results can be combined into a single result set .
1.1 Use union
union Is very simple to use , Just specify each select sentence , And put keywords between them UNION.
Let's take a look at an example :
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);
The above statement uses where Clause substitution “:
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
OR vend_id IN (1001,1002);
1.2 UNION The rules
Here are some UNION The rules :
- union There must be two or more select Sentence composition , Every statement uses keywords union Separate ( therefore , If you combine 4 strip select sentence , Use 3 individual UNION keyword ).
- UNION Each query in the must contain the same columns 、 Expression or aggregate function ( Although the columns do not have to be listed in the same order ).
- Column data types must be compatible . They don't have to be exactly the same type , But they have to be Oracle Types that can be implicitly converted ( for example : Different numeric types or different date types ).
1.3 Include or eliminate duplicate rows
union Any duplicate rows will be automatically deleted from the query result set ( let me put it another way , It's like a single SELECT Multiple in the statement WHERE The clauses and conditions are the same ). But if you need to , It can also change him . in fact , If you really want to return all the matches that appear , have access to UNION ALL Instead of UNION.
UNION ALL Examples of use :
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);
Tips :
UNION Almost complete with multiple where Things with the same conditions .union all yes union A form of , It can be completed but not used where What clause does . in fact , If you really want to return all matches for each condition ( Include duplicate lines ), Must use union all, Instead of using where.
1.4 Sort the results of the combined query
SELECT The output of the statement is to use ORDER BY Clause . When using UNION When combining queries , Only one ORDER BY Clause , And it must appear the last SELECT After statement .
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;
This UNION In the second SELECT The statement is followed by a single ORDER BY Clause . Even if ORDER BY It seems to be just the last SELECT Part of the statement ,Oracle In fact, it will also be for all SELECT Statement to sort all the results returned .
边栏推荐
- 用户解锁SM04 SM12
- Linear table -- stack and queue
- 12. Integer to Roman整数转罗马数字
- Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
- 冰冰学习笔记:类与对象(中)
- Gossip: Recently, many friends talk about going abroad
- (2022 Hangdian multi school III) 1009.package delivery (greedy)
- Shell系统学习之Shell条件测试,判断语句和运算符
- Bash: 创建返回布尔类型值的函数
- 单元测试系统化讲解之Mockito
猜你喜欢
随机推荐
杂谈:最近好多朋友谈出国……
A small cotton padded jacket with air leakage
杂谈:高考
优炫数据库主要线程有哪些?
【QT】capture.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_htons(解决方法)
docker安装MySQL8.0.28
Logcat tool
临界区(critical section 每个线程中访问 临界资源 的那段代码)和互斥锁(mutex)的区别(进程间互斥量、共享内存、虚拟地址)
Excuse me, is there a big delay in individual capture when someone uses Oracle xStream? How to solve the delay problem
Using loops to process data in tables in kettle
Usage of string class
Array method and loop in JS
vlan间路由(讲解+验证)
C4D动画如何提交云渲染农场快速渲染?
在rhel7.3中编译和使用log4cxx
Examples of Oracle triggers
(2022 Hangdian multi school III) 1011.taxi (Manhattan maximum + 2 points)
一款开源 OA 办公自动化系统
Quartus: an error is reported when adding a.V file to someone else's project
Zabbix: 将收集到值映射为易读的语句









