当前位置:网站首页>Oracle: subquery, sorting
Oracle: subquery, sorting
2022-06-09 07:26:00 【Cat fearless mouse a】
CMD Command line connection Oracle database
1、 Use CMD Command line connection Oracle The database should be connected to MySQL The database is almost , So here is a brief introduction
2、 command :conn user name / password @ Where the database is IP: Port number / Instance name
⑴ Because in Oracle Database corresponding to the user in , So use different usernames to connect to different databases
example 0:
C:\Users\Administrator>sqlplus/nolog
SQL*Plus: Release 11.2.0.1.0 Production on Monday 9 month 14 20:10:51 2020
Copyright (c) 1982, 2010, Oracle. All rights reserved.
SQL> conn zh/[email protected]:1521/orcl
Connected .
SQL>
Sort
1、 stay SQL Can be used in ORDER BY Sort query results : Here with MYSQL The grammar in is the same
2、 Complete grammar :SELECT Name FROM Table name where Filter conditions ORDER BY Sort column names 1| Sort column names 2 ASC|DESC
3、 Parameters :
⑴ Sort column names 1| Sort column names 2: Indicates which columns are sorted by , According to one or more
⑵ASC: In accordance with the said ASCII Ascending order ( This is the default sort method , You don't specify )
⑶DESC: In descending order
example 1:

example 1_1: Default sort ( Ascending )

example 1_2: Descending

example 1_3: Multiple sorting

notes :
When there are multiple sort column names : If the first parameter has multiple rows with the same value, it will be sorted by the second parameter , If the value of the first parameter is unique , Then it will not be sorted by the second parameter
Limit the number of returned queries
1、 I was learning MySQL when , Introduced a LIMIT Clause : This clause is in MYSQL To limit the number of returned query results
⑴ Such as : There are many pieces of data that meet the query conditions , Then you can use LIMIT Clause to limit the number of actual returned results
2、 however : stay Oracle No, LIMIT Clause
⑴Oracle: from 12c Version start , Can be used FETCH Clause limits the number of rows returned
3、 So in 12c How to limit the number of query results before ? have access to ROWNUM!!
4、 about rownum It is oracle The system is sequentially assigned the number of rows returned from the query , The first line returned allocates 1, The second line is 2, And so on
⑴ This pseudo field can be used to limit the total number of rows returned by the query , And rownum Cannot prefix with the name of any table
⑵ This rownum Clause is a bit special , I don't think it's easy to understand , There are a lot involved , Specifically, you can baidu yourself
example 2: Only the first data is returned

notes : About limiting the number of query results , You can see https://blog.csdn.net/mitedu/article/details/3584399
Subquery
1、SELECT The result of the query is another SELECT、UPDATE or DELETE The condition of the statement , Its essence is to WHERE A conditional expression nested within a condition
2、 Subqueries are nested in SQL SELECT Another in the statement SELECT sentence
⑴ Subquery ( Internal query ) Execute before the main query is executed ( Subqueries must be in parentheses )
⑵ Main query ( External query ) Use the results of the subquery
3、Oracle Sub queries are divided into two types : One is single line sub query , One is multi row subquery ( Another kind is empty )
⑴ Single line sub query : Inner layer of single row subquery SELECT Statement returns only one row of data , That is to say, it is embedded in other Sql The one in the statement select The query value returns a row of data
⑵ Multi line sub query : Multi row subqueries are embedded in other Sql Statement SELECT Query returns multiple rows of data
4、 Note that the subquery returns multiple rows : Outer layer SELECT The filter condition of can only be used IN keyword , Out of commission '='
⑴'=' The sign means one-to-one ,in Means in a range ( Used to process multi row records returned from sub queries )
5、 Subqueries are generally used for multi table queries : There are associated fields between multiple tables , Query the data of another table through the associated fields of one table
Single table subquery
Single table subquery : That is, all queries are performed in one table
notes : The following query is based on the following data

Single line sub query
Single line sub query : Inner layer of single row subquery SELECT Statement returns only one row of data , That is to say, it is embedded in other Sql The one in the statement select The query value returns a row of data
example 4:

example 4_1:

example 4_2:

example 4_3:

Multi line sub query
1、 Multi line sub query : Multi row subqueries are embedded in other Sql Statement SELECT Query returns multiple rows of data
2、 If the subquery returns multiple rows , Then the filter criteria of the main query can only use in 了 , Cannot use equal to 、 Greater than 、 Less than equal
example 4_4: The wrong sample

example 4_5:

Multi table subquery
1、 Multi table subquery : There is a certain field connection between two tables , Query the data of another table through one table
2、 In multi table subquery , The data returned by the sub query can also be a single line , It can also be multiple lines
example 5: Single line sub query

example 5_1: Multi line sub query

Oracle Function introduction
NVL function
1、 Format :NVL(expr1,expr2)
2、 effect : It can be used to judge whether the query result is empty ( It means that the query column value is null, Not that there are no query results )
⑴ If the first parameter (expr1: expression ) The value of is empty , Then return the second parameter (expr2: expression ) Value ( The first parameter is always the table query field )
⑵ If the first parameter ( expression ) The value of is not empty , Then return the first parameter ( expression ) The original value
⑶ The two parameter types can be any type , or NULL
example 6:

example 6_1:

NVL2 function
1、 Format :NVL2(expr1,expr2, expr3)
2、 effect : And NVL Function similar to , There are only three parameters
⑴ If the first parameter of the function is not empty , Then return the value of the second parameter
⑵ If the value of the first parameter of the function is null , Then return the value of the third parameter
example 7:

example 7_1:

边栏推荐
- How to solve security problems in low code platforms?
- MongoDB基础理解
- Classification explanation of compatibility test
- 知行之桥EDI系统Shopify端口的使用
- 2022制冷与空调设备安装修理特种作业证考试题库及答案
- Pushmall push and paste sharing e-commerce plan update completed in May 2022
- 招聘 | 上班轰趴,下班狼人杀,天天招人,怕是要发!
- 100 code structure and how to avoid them (4)
- 1. Talking about the system construction model -- one of the learning directions of advanced management post for testers
- Multithreading - the concept of program process threads
猜你喜欢

2022 Chinese cook (elementary) examination question bank and online simulation examination
![[path of system analyst] Chapter 15 double disk database system (relational database application)](/img/c8/7e8b4356ce8ddf0e008abe0ad88f05.jpg)
[path of system analyst] Chapter 15 double disk database system (relational database application)

How idea views the path to save code

PostgreSQL database semaphore mechanism pgsemaphore underlying principle

mysql根据父节点递归查询所有子节点,List转树形结构工具类

使用postman模拟携带token的请求
![[PPO attitude control] Simulink simulation of UAV attitude control based on reinforcement policy optimization PPO training](/img/f7/032eb82cdf0bc5c564cc509d02b0c6.png)
[PPO attitude control] Simulink simulation of UAV attitude control based on reinforcement policy optimization PPO training

分布式消息中间件--消息队列介绍

InfoWorld文章丨将数据编排技术用于AI模型训练

Distributed message middleware -- Introduction to message queue
随机推荐
Peripheral driver library development notes 42:dac8552 DAC driver
pushmall推贴共享电商2022年5月份计划更新完成
Some pitfalls encountered in using Renren open source project
Summarize the key knowledge of multithreading.
A collection of common test terms
[PPO attitude control] Simulink simulation of UAV attitude control based on reinforcement policy optimization PPO training
7. Safety knowledge
当内卷风波及代码领域,看Alluxio将会采取怎样的块分配策略
【无标题】
Summarize 2021 Test Engineer Interview Questions (including answers)
招聘 | 上班轰趴,下班狼人杀,天天招人,怕是要发!
朴素贝叶斯分类器
WPF 数据绑定(二)
Mongodb basic understanding
多线程重点知识归纳总结。
2022年中式烹调师(初级)考试题库及在线模拟考试
PostgreSQL数据库复制——后台一等公民进程WalReceiver 提取信息
Realization of C 2~36 hexadecimal number conversion
如何防范最大的云安全威胁
Talk about the ten mistakes often made in implementing data governance