当前位置:网站首页>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:

 

 

原网站

版权声明
本文为[Cat fearless mouse a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021422096433.html