How to apply MySQL high-order statements (subqueries)

Table of contents

1. SQL high-level statements

Common queries

Keyword sorting

ascending order descending order

Search by area

Group statistics

limit limit the displayed result entries

Asalias settings

scenes to be used

Nested cloning copies table structure

 2. Wildcard

3. Subquery

insert subquery

update subquery

delete subquery

Exists detection


1. SQL high-level statements

Common queries

For MySQL database queries, in addition to basic queries, sometimes it is necessary to process the query result set, such as fetching only 10 pieces of data, sorting or grouping the query results, etc.

Keyword sorting

ascending order descending order

Use the Order by statement to implement sorting, which can be sorted on one or more fields.

ASC (ascending order)

DESC (descending order)

Syntax structure: select [field] ······· from [table name] where [field]='value' order by [field] [arrangement]

Search by area

Syntax format: select [field],······· from [table name] where [conditional judgment] and/or [conditional judgment]

Group statistics

Count the number of fields ① corresponding to each field ② in the specified table (the table name can be followed by a where condition)

Syntax format: select count (field ①), [field] from [table name] group by [field ②];

limit limit the displayed result entries

Syntax format: select [field] from [table name] limit [offset], [number of rows displayed];

Asalias settings

Syntax format: select [field] as [alias], [field] as [alias] ······· from [table name];

The table name specified by From can also use As to set an alias.

scenes to be used

Can also be used as a connector

① When querying complex tables, aliases can shorten the length of the query statement.

② When querying multiple tables (easy to understand, short SQL statement)

Nested cloning copies table structure

Where judgment statements can be added

Syntax format: create table [table name] (select * from [table to be cloned]);

 2. Wildcard

Wildcards are mainly used to replace some characters in a string, and query related results by matching some characters.

Usually wildcards are used together with LIKE, and together with the WHERE clause to complete the query task. There are two commonly used wildcard characters:

%: The percent sign represents zero, one or more characters_ 
: The underscore represents a single character

For example, I want to query the rows whose Name field starts with lowercase 'l'

Command: select [field] from [table name] where name like 'l%';

3. Subquery

Also called inner query or nested query, it means that within a query statement, another query statement is nested. The subquery statement is executed before the main query statement. It can query the same table or different tables.

For example, I want to first check the data with id greater than 2, and then filter the data with age>22 based on the data with id greater than 2 and display its id, name and age fields.

Syntax format: select [field] from [table name] where [field] in (select [field] from [table name] where [conditional judgment]);

Subqueries can also be used in (insert, update, delete). Subqueries can also continue to nest subqueries internally, that is, they can be nested at multiple levels.

"in" is used to determine whether a certain value is in the given result set, usually used in conjunction with a subquery.

insert subquery

First find the table name field data in the subquery, then find the field data in the specified table that is the same as the subquery table name field data, and insert all field data with the same fields into the specified table.

Syntax format: insert into [table name] select * from [table name] where [field] in (select [field] from [table name]);

update subquery

First query the data that the fields in the specified table want to match, then use it as the matching condition to modify the table, and modify the related specified field data based on the same fields.

Syntax format: update [table name] set [field]='value' where [field] in (select [field] from [table name] where [field]='value');

delete subquery

First query the name field in the specified table, and then use it as the matching condition of the main query to delete the entire row of data.

Syntax format: delete from [table name] where [field] in (select [field] from [table name] where [field]=value);

Exists detection

It has the same effect as "in". If the corresponding field is matched, all data in the table will be returned. If the match fails, none of the results will be returned, and no result will be returned.

Syntax format: select [field] from [table name] where exists (select [field] from [table name] where [field]=value);

Guess you like

Origin blog.csdn.net/Liu_Fang_Hong/article/details/131807006