当前位置:网站首页>Mysql database easy learning 06 - commonly used by data analysts: single table query of data query language DQL

Mysql database easy learning 06 - commonly used by data analysts: single table query of data query language DQL

2022-06-22 01:58:00 Smart Aries

Data query language DQL

Used to query records in database tables
The basic structure :

SELECT  Field name  FROM  Table or view name  WHERE  Query criteria 

Single table query

1、 The result is a virtual result set

select After the statement is executed, the server will retrieve the data in the table as required , And send the search results to the client , This temporary result set in the form of a table , It is Store in memory , Not on disk , After performing other operations, the result set is gone , So it is A temporary virtual result set , Not a real table .

2、 A full table query

select * from  Table name ; 

If a table has a large volume , Generally, the following two methods are used to view the data in the table

1、 Choose the former 10 Row to view top 10

select top 10 *  from  Table name 

2、 Limit the number of query results limit

select  Field 1[, Field 2,] from  Table name  limit [ Offset ,]  Row number ; 

limit Accept One or two numbers Parameters , Argument must be an integer constant
The first parameter specifies the offset of the first return record line , The second parameter specifies the maximum number of rows to return records
If only one parameter is given , Indicates the maximum number of record lines returned
The offset of the initial record line is 0( instead of 1)
Example :
dept There is a total of 4 Row data
 Insert picture description here  Insert picture description here
 Insert picture description here

3、 Query specified column

select  Field 1[, Field 2,] from  Table name ; 

4、 Query non duplicate data distinct

select distinct  Field name  from  Table name ; 

5、 Set alias as

select  Field name  as  Column alias  from  Original table name  [as ] Table alias ;

6、 Conditions of the query where

select  Field 1[, Field 2,] from  Table name  where  filter ; 

You can add... To the filter criteria Operator
Arithmetic operator :
+ Add ; - reduce ;* ride ; / except

Comparison operator :
= be equal to ;>/>= Greater than / Greater than or equal to ;</<= Less than / Less than or equal to ;
!=/<> It's not equal to ;
between…and… Between what

Logical operators
and And ;or or ;not Not

7、 Null value query is null

select  Field 1[, Field 2,] from  Table name  where  Null value field  is[not] null; 

Be careful : Null values cannot be compared , If you want to filter out null values, you should use is null

8、 Fuzzy query like wildcard

select  Field 1[, Field 2,] from  Table name  where  String field [ not] like  wildcard ; 

wildcard :
Percent sign (%) wildcard : matching 0 Characters or more
Underline (_) wildcard : Match a character

9、 Aggregate operations

Aggregate multiple rows of data into one row
The aggregate function will do null Other data Aggregate operations
 Insert picture description here

10、 Grouping query and grouping filter

Group query

select  Field 1[, Field 2,] from  Table name [ where  Query criteria ] group by  Grouping field 1[, Grouping field 2,];

Group query results by one or more fields , The fields with the same value are a set of , Aggregate each group
When grouping multiple fields , Multiple fields with exactly the same value will be divided into a group
Filter after grouping :

select  Field 1[, Field 2,] from  Table name [ where  Query criteria ][ group by  Grouping field 1[, Grouping field 2,]] having  filter ; 

where And having The difference between
where In grouping and aggregation Computing Before Filter the data in the table , and having In grouping and aggregation after Filter the divided groups , therefore where Clause cannot contain aggregate function .

11、 Sorting of query results order by

select  Field 1[, Field 2,] from  Table name  order by  Field 1[  Sort direction , Field 2  Sort direction ,]; 

Sort query results by one or more fields , When sorting, you can specify the sorting direction for each field . When sorting multiple fields , First, sort by the first field , If the first field has the same value, sort by the second field
Specify sorting direction
asc( ascendent) Ascending , for example [ 0 1 2 3 4 5 ];
desc(descendent) Descending , for example [ 5 4 3 2 1 0 ]

原网站

版权声明
本文为[Smart Aries]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220134337177.html