SQL Notes (1) SQL syntax, SELECT statement, DISTINCT statement, LIMIT, LIMIT and OFFSET combined use, WHERE clause

Article directory

1SQL syntax

1.1 SELECT statement

SELECT
[ALL|DISTINCT]
<target column expression 1> [alias],
<target column expression 2> [alias]…
FROM <table name or view name> [alias],<table name or view name> [ alias]…
[WHERE<conditional expression>]
[GROUP BY <column name>
[HAVING <conditional expression>]]
[ORDER BY <column name> [ASC|DESC]]
[LIMIT <number or list>];
simplify version syntax
SELECT *| column name FROM table WHERE condition

1.1.1SQL SELECT statement

The SELECT statement is used to select data from a table.
The results are stored in a results table (called a result set).

1.1.2SQL SELECT syntax

SELECT 列名称 FROM 表名称

as well as:

SELECT * FROM 表名称

SQL 语句对大小写不敏感。SELECT 等效于 select。提示:星号(*)是选取所有列的快捷方式。

1.2 DISTINCT statement

1.2.1 The role of DISTINCT

In MySQLl, DISTINCTthe main function of keywords is to filter the repeated data of one or more fields in the database table, and only return one of the data to the user, DISTINCTwhich can only be SELECTused in

1.2.2 Usage of DISTINCT

SELECT DISTINCT expression[,expression...]
FROM tables 
[where conditions];

In the process of using distinct, pay attention to the following points:

  1. When deduplicating fields, make sure that DISTINCT is at the top of all fields
  2. If there are multiple fields after the DISTINCT keyword, the multiple fields will be combined and deduplicated, and only the combined values ​​of multiple fields will be deduplicated.

1.2.3 Principle of DISTINCT

The main principle of DISTINCT deduplication is to group the data to be deduplicated first, and then return one of the grouped data to the client. There may be two different situations in this grouping process. :

  1. The fields that DISTINCT depends on do not all contain indexes:
    in this case, because the index cannot satisfy the entire process of deduplication, a temporary table is needed. MySQL first needs to put the data that meets the conditions into the temporary table, and then put the data in the temporary table. Part of the data is grouped, and then one piece of data is removed from each grouped data in the temporary table. The data is not sorted during the grouping process in the temporary table.

  2. The fields that DISTINCT depends on all contain indexes:
    in this case, MySQL directly groups the data that meets the conditions by operating the indexes, and then removes a piece of data from each group of data after the grouping.

1.3LIMIT

When there are tens of thousands of pieces of data in the data table, querying all the data in the table at one time will slow down the speed of data return and cause great pressure to the database server. At this time, you can use the LIMIT keyword to limit the number of items returned by the query result.

LIMIT is a special keyword in MySQL, which is used to specify which record the query result starts to display and how many records are displayed in total.

The LIMIT keyword can be used in three ways: specifying the initial position, not specifying the initial position, and combining with OFFSET.

1.3.1 Specify the initial position

The LIMIT keyword can specify which record the query result starts to display and how many records are displayed.
The basic syntax format of LIMIT specifying the initial position is as follows:

LIMIT 初始位置,记录数

Among them, "initial position" indicates from which record to display; "number of records" indicates the number of displayed records. The position of the first record is 0, and the position of the second record is 1. The following records follow and so on.

Notice:LIMIT 后的两个参数必须都是正整数。

1.3.1.1 Example: In the emp table, use the LIMIT clause to return records with a row number of 5 starting from the third record, the SQL statement and the running results are as follows

insert image description here

insert image description here
As you can see from the results, the statement returns 5 records starting from the 3rd record. The first number "2" after the LIMIT keyword means to start at row 3 (record position starts at 0, row 3 is at position 2), and the second number 5 means the number of rows returned.

1.3.2 Do not specify the initial position

When the LIMIT keyword does not specify an initial position, records are displayed starting from the first record. The number of records displayed is specified by the LIMIT keyword.

The basic syntax of LIMIT without specifying the initial position is as follows:

LIMIT 记录数

Among them, "number of records" indicates the number of displayed records. If the value of "Number of records" is less than the total number of query results, the specified number of records will be displayed starting from the first record. If the value of "Number of Records" is greater than the total number of query results, all the queried records will be displayed directly.

1.3.2.1 The example shows the first 3 rows of the query result of the emp table, the SQL statement and the running result are as follows

insert image description here
insert image description hereOnly 3 records are displayed in the result, indicating that "LIMIT 3" limits the number of displayed records to 3.

1.3.3 Combination of LIMIT and OFFSET

LIMIT can be used in combination with OFFSET, the syntax is as follows:

LIMIT 记录数 OFFSET 初始位置

The parameters have the same meaning as those in the LIMIT syntax. "Initial Position" specifies which record to start displaying; "Number of Records" indicates the number of displayed records.

1.3.3.1 In the emp table, use LIMIT OFFSET to return 5 records starting from the 4th record. The SQL statement and the running result are as follows.

insert image description here

insert image description here

As can be seen from the results, the statement returns 5 records starting from the 4th record. That is, "LIMIT 5 OFFSET 3" means to get the next 5 records starting from the 4th record, which is the same as the result returned by "LIMIT 3, 5".

1.3.4 Summary

LIMIT with one parameter specifies to start from the first row of the query results, and the only parameter is the number of rows returned, 即“LIMIT n”、"LIMIT n OFFSET 0"与“LIMIT 0,n”返回结果相同. LIMIT with two arguments returns the specified number of rows of data starting at any position.

But: LIMIT 0, n is more efficient
LIMIT n is less efficient
LIMIT n OFFSET 0 is running efficiently

SELECT *
FROM emp 
LIMIT 0,2 运行效率更高
SELECT *
FROM emp 
LIMIT 2   运行效率低
SELECT *
FROM emp 
LIMIT 2 OFFSET 0 运行效率中

1.4 WHERE clause

WHERE clause to filter records

1.4.1 Operators in the WHERE clause

The following operators can be used in the WHERE clause:

operator describe
= equal
<> not equal to. Note: In some versions of SQL, this operator can be written as !=
> more than the
< less than
>= greater or equal to
<= less than or equal to
BETWEEN within a certain range
LIKE search for a pattern
IN Specify multiple possible values ​​for a column

1.4.2 Examples

1.4.2.1emp table

insert image description here

1.4.2.2 To query the job in the emp table as artificial intelligence, use "="

SELECT empno,job 
FROM emp
WHERE job = "人工智能"

insert image description here

1.4.2.3 If the sal in the emp table is greater than 20000, use ">","<",">=","<=" is also similar

SELECT ename,sal 
FROM emp
WHERE sal > 20000;

insert image description here

1.4.2.4 Query the use of "<>" when deptno is not equal to 20 in the emp table

SELECT ename,deptno 
FROM emp
WHERE deptno <> 20;

insert image description here

1.4.2.5 Query the value of sal in the emp table between 20000-40000, the use of "between"

SELECT ename,sal 
FROM emp
WHERE sal BETWEEN 20000 AND 40000;

insert image description here

1.4.2.6 Query the use of the word "end" in the job in the emp table and the keyword "like"

SELECT ename,job 
FROM emp
WHERE job LIKE "%端%";

insert image description here

1.4.2.7 Query the deptno in the emp table with 10, 30, 50

SELECT ename,deptno 
FROM emp
WHERE deptno IN(10,30,50);

insert image description here

1.4.2.8 Query without comm value

SELECT *
FROM emp
WHERE comm IS NOT null; 

insert image description here

1.4.2.9 Multiple condition query

The logical operators AND and OR can be used in the WHERE clause to compose multi-condition queries.
The syntax for using AND predicates is as follows:
boolean expression 1 AND boolean expression 2 AND ... AND boolean expression n
The result of the entire expression is true only if all boolean expressions are true, as long as there is one boolean expression If the result of the expression is false, the result of the entire expression is false.

The syntax for using the OR predicate is as follows.
Boolean expression 1 OR Boolean expression 2 OR ... OR Boolean expression n
means that as long as one of the Boolean expressions is true, the result of the whole expression is true; only when the result of all the Boolean expressions is false, the whole expression is false. The expression result is false.

1.4.2.9.1 Query deptno is 20 and sal>20000
SELECT *
FROM emp
WHERE deptno = 20 AND sal > 20000;

insert image description here

1.4.2.9.2 Query deptno as 20 or sal>20000

insert image description here

Guess you like

Origin blog.csdn.net/Redamancy06/article/details/126513682