当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
闭散列和开散列解决哈希冲突
(2022 Niuke multi school III) a-ancestor (LCA)
Confluence漏洞学习——CVE-2021-26084/85,CVE-2022-26134漏洞复现
用户解锁SM04 SM12
C4D云渲染平台选哪家合作?
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
MySQL2
在Perl程序中暴露Prometheus指标
functools模块
(posted) comparison of Eureka, consumer and Nacos 1
C程序代码的内存结构分析
(2022牛客多校三)J-Journey(dijkstra)
Port forwarding summary
C4D动画如何提交云渲染农场快速渲染?
No.0 training platform course-2. SSRF Foundation
Guava的基础功能与集合
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
Quickly update the information in a field in kettle
when的多条件查询
36 - new promise method: allsettled & any & race









