MySQL DQL data query

1.SELECT statement

MySQL's SELECT statement is used to retrieve data from a database table. Powerful functions, complex and diverse statement structures. But the basic statement format is as follows.

SELECT [列名称] FROM [表名称] WHERE [条件]

A complete SELECT statement contains optional clauses. The SELECT statement is defined as follows:

SELECT clause
[FROM clause]
[WHERE clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause]
[LIMIT clause]
  1. The SELECT clause is required, the other clauses are optional.

A SELECT can be calculated without referencing any table, that is, without any other words, only the SELECT clause.

SELECT 1 + 1 AS sum;
+-----+
| sum |
+-----+
|   2 |
+-----+
  1. In a SELECT statement, the order of clauses is fixed. For example, the GROUP BY clause will not be placed in front of the WHERE clause.
  2. The execution order of the different clauses of the SELECT statement:
开始 > FROM子句 > WHERE子句 > GROUP BY子句 > HAVING子句 > SELECT子句 > ORDER BY子句 > LIMIT子句 > 最终结果

After each clause is executed, an intermediate data result is generated, which is the so-called temporary view, which is used by the following clauses. If a clause does not exist, it will be skipped.

It should be noted that there may be some differences between different database management systems, but in general, the above order applies to most SQL queries.

The execution order of MySQL and standard SQL is basically the same.

2.SELECT clause

The SELECT clause is used to specify columns to select or to use expressions to generate new values.

For the selected data, you can also add some modifications, such as using the DISTINCT keyword for deduplication.

A complete SELECT clause consists of the following.

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
    [HIGH_PRIORITY]
    [STRAIGHT_JOIN]
    [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
    [SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr] ...
    [into_option]

into_option: {
    INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        export_options
  | INTO DUMPFILE 'file_name'
  | INTO var_name [, var_name] ...
}

Select_expr is required and represents the column or expression to be queried or * represents all columns.

SELECT * FROM t1 INNER JOIN t2 ...

You can use functions to operate on columns, and use the AS keyword to name the result columns (AS is optional and can be omitted).

SELECT AVG(score) AS avg_score, t1.* FROM t1 ...

# 或
SELECT AVG(score) avg_score, t1.* FROM t1 ...

3.FROM clause

The FROM clause indicates the table from which rows are to be retrieved. If multiple tables are named, a join is performed. For each table specified, you can optionally specify an alias.

FROM table_references [PARTITION partition_list]

SELECT supports explicit partition selection using the PARTITION clause, with the name of the table_references table followed by a list of partitions or subpartitions (or both). In this case, only rows from the listed partitions are selected and ignored. Any other partition of the table. For information on partitioning, please refer to Chapter 24 Partitioning .

4.WHERE child clause

If a WHERE clause is given, indicates one or more conditions that a row must meet to be selected. where_condition is an expression that evaluates to true for each row to be selected. Without a WHERE clause, all rows are selected.

[WHERE condition]

The following operators can be used in conditional expressions in the WHERE clause.

operator describe
= equal
!= or <> not equal to
> more than the
< less than
>= greater or equal to
<= less than or equal to
BETWEEN AND Within a certain range (closed interval)
LIKE Search for a pattern
AND Multiple conditions and
OR multiple conditions or

(1) Usage of WHERE IN

There are two main uses of IN in the WHERE clause:

  • IN is followed by the record set generated by the subquery. Note that the subquery result data column can only have one column and there is no need to add an alias to the subquery result set.
SELECT * FROM tbl_name1 WHERE col_name1 IN (SELECT col_name2 FROM tbl_name2); 
  • IN is followed by the data collection.
SELECT * FROM  tbl_name  WHERE  col_name  IN ('foo', 'bar', 'baz', 'qux'); 

Note: If the data type is a string, be sure to enclose the string in single quotes.

5.GROUP BY child clause

The data columns in the GROUP BY clause should be all columns in the data columns specified by SELECT, unless this column is used for aggregate functions, such as SUM(), AVG(), COUNT(), etc.

However, if the data column specified by SELECT is not used in the aggregate function and is not in the GROUP BY clause, it stands to reason that an error will be reported, but MySQL will select the first column to be displayed in the result set.

# 选择发起加好友请求次数超过10次的QQ(uin),被加方(to_uin)只会显示第一个
SELECT uin, to_uin, count(*) AS cnt from inner_raw_add_friend_20170514 GROUP BY uin HAVING cnt>10;

6.HAVING clause

HAVING, like the WHERE clause, is used to specify selection conditions. However, there are obvious differences in the usage of HAVING and WHERE clauses.

  1. The objects of action are different.

WHERE works on tables and views, HAVING works on groups.

# 查询 QQ 3585076592 和 3585075773 在 20170514 当天加好友请求次数且请求次数>10
SELECT uin,count(*) AS cnt
FROM inner_raw_add_friend_20170514
WHERE uin=3585076592 OR uin=3585075773
GROUP BY uin HAVING cnt>10;
  1. The stages of action are different.

WHERE selects input rows before grouping and aggregation calculations (therefore, it controls which rows enter the aggregation calculation), while HAVING selects grouping after grouping and aggregation. Therefore, the WHERE clause cannot contain an aggregate function because it would be pointless to try to use an aggregate function to determine which rows are input to an aggregation operation. In contrast, the HAVING clause typically contains aggregate functions. Of course, you can also use HAVING to filter the result set, but this is not recommended. The same conditions can be used more effectively in the WHERE stage.

# 查询指定 QQ 加好友请求信息(where作用于输入阶段的数据集)
SELECT * FROM inner_raw_add_friend_20170514 WHERE uin=3585078528;

# 作用等同于 WHERE, 但 HAVING 作用于结果阶段的结果集
SELECT * FROM inner_raw_add_friend_20170514 HAVING uin=3585078528;

7.ORDER BY child clause

The ORDER BY clause is used to sort the result set based on specified columns.

[ORDER BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]

The ORDER BY statement sorts records in ascending order ASC (ascend) by default. If you want to sort in descending order, you can use the DESC (descend) keyword and use the random number function randomly RAND().

Using column position (1-based) is deprecated when specifying columns to sort on, as this syntax has been removed from the SQL standard.

For example, sort by QQ number in descending order.

SELECT * FROM inner_raw_add_friend_20170514 ORDER BY uin DESC;

8.LIMIT clause

The LIMIT clause can be used to force a SELECT statement to return a specified number of records.

[LIMIT {
   
   [offset,] row_count | row_count OFFSET offset}]

LIMIT accepts one or two numeric arguments. The parameter must be an integer constant. If two parameters are given, there are two usages.

offset,row_count
# 或
row_count OFFSET offset

offset is the starting offset of the returned record rows, starting from 0, and row_count is the maximum number of returned record rows.

Only one parameter is given, indicating the maximum number of top rows to return. The starting offset defaults to 0.

Return all remaining records starting from the starting offset. You can use some very large second parameter. For example, retrieve all lines from line 96 to the last line.

SELECT * FROM tbl LIMIT 95,18446744073709551615;

Note that MySQL currently does not support using -1 to indicate returning all remaining records starting from the offset, that is, the following writing is wrong:

SELECT * FROM tbl LIMIT 95,-1

9.DISTINCT clause

The DISTINCT keyword is used to remove duplicate rows from query results and only return unique rows.

(1) Use DISTINCT combined with the COUNT() function to count the number of unique records.

# 选择每一个 QQ 发起加好友请求涉及到的不同的 QQ 数
SELECT uin, count(distinct to_uin) c FROM add_friend GROUP BY uin;

(2) DISTINCT is used to select different records, and can only be placed at the beginning of the selected column and affects all subsequent columns.

# 查询 uin 和 to_uin 不重复的加好友请求
SELECT DISTINCT uin, to_uin FROM add_friend;

# 示例数据表
uin      to_uin
10000    123456
10000    121212
10001    121212
10001    131313

# 结果集
uin      to_uin
10000    123456
10000    121212
10001    121212
10001    131313

If you want the function of DISTINCT to act on to_uin of the second column, it is hopeless to use DISTINCT because MySQL syntax is not yet supported. You can use GROUP BY instead.

SELECT uin, to_uin FROM add_friend WHERE GROUP BY to_uin;

# 结果集
uin      to_uin
10000    123456
10000    121212
10001    131313

This trick can only be used in MySQL, because standard SQL syntax stipulates that columns in non-aggregate functions must be in the GROUP BY clause. MySQL stipulates that when the column in the non-aggregate function does not exist in the GROUP BY clause, the first row of each group is selected.

(3) COUNT DISTINCT counts the number of records that meet the conditions.

If you want to perform COUNT DISTINCT on records that meet the conditions, how to add conditions?

See MySQL distinct count if conditions unique , you can use the following method.

COUNT(DISTINCT CASE WHERE 条件 THEN 字段 END)

See mysql count if distinct , you can also use the following method.

COUNT(DISTINCT col_name1, IF(col_name2=1, true, null))

10.UNION child clause

The function of UNION is to vertically combine the results of two or more queries.

query_expression_body UNION [ALL | DISTINCT] query_block
    [UNION [ALL | DISTINCT] query_expression_body]
    [...]

Here's an example.

mysql> SELECT 1, 2;
+---+---+
| 1 | 2 |
+---+---+
| 1 | 2 |
+---+---+
mysql> SELECT 'a', 'b';
+---+---+
| a | b |
+---+---+
| a | b |
+---+---+
mysql> SELECT 1, 2 UNION SELECT 'a', 'b';
+---+---+
| 1 | 2 |
+---+---+
| 1 | 2 |
| a | b |
+---+---+

You need to pay attention to the following points when using UNION.

(1) Conditions of use of UNION

UNION can only be applied to the result set and cannot be applied directly to the original table. As long as the number of columns in the result set is the same, it can be used even if the field types are different. It is worth noting that the name of the field after UNION shall be based on the first SQL.

(2) The difference between UNION and UNION ALL

UNION is used to merge the result sets of two or more SELECT statements and eliminate duplicate rows after the merge. UNION ALL retains duplicate rows.

(3) About the sorting of UNION

There are two tables with the following contents:

# table1
uin		nickname
10001	monkey
10002	monkey king

# table2
uin		nickname
20000	cat
20001	dog

Sort the two result sets in descending order by uin and then combine them.

(SELECT * FROM table1 ORDER BY uin DESC) UNION (SELECT * FROM table2 ORDER BY uin DESC);

uin		nickname
10001	monkey
10002	monkey king
20000 	cat
20001 	dog

It can be found that the inner sorting does not work, so now try the outer sorting.

SELECT * FROM table1 UNION SELECT * FROM table2 ORDER BY uin DESC;

uin		nickname
20001 	dog
20000 	cat
10002	monkey king
10001	monkey

It can be seen that the outer sorting takes effect. Does that mean that the inner sorting is useless? In fact, from another perspective, think about the inner sorting first. If the outer sorting is also sorted, the inner sorting is obviously redundant, so MySQL optimizes the SQL statement to prevent the inner sorting from taking effect. For inner sorting to work, the result of the inner sorting must affect the final result, such as adding LIMIT.

(SELECT * FROM table1 ORDER BY uin DESC LIMIT 2)
UNION
(SELECT * FROM table2 ORDER BY uin DESC LIMIT 2);

uin		nickname
10002	monkey king
10001	monkey
20001 	dog
20000 	cat

In addition, there is an essential difference that we must know when using UNION and JOIN.

UNION can only act on SELECT result sets and cannot directly act on data tables, while JOIN, on the contrary, only acts on data tables and cannot directly act on SELECT result sets (you can specify an alias for the SELECT result set as a derived table).

11. View the number of records in the data table

There are several ways to view the number of rows in a data table.

  1. Use COUNT(*)
SELECT COUNT(*) FROM tbl_name;

It is recommended to use the MyISAM data table because the MyISAM data table caches the row numbers in advance and can be obtained directly. It is not recommended to use InnoDB data tables. When the number of rows in the data table is too large, the query will be slow because the entire table needs to be scanned.

  1. View the system table information_schema.TABLES
SELECT table_rows
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='db_name' AND TABLE_NAME='tbl_name';

information_schema is a system database in MySQL, which contains metadata information about databases, tables, columns, etc. The number of records in the specified data table can be obtained by querying the information_schema.TABLES table.

  1. Use the SHOW TABLE STATUS command
SHOW TABLE STATUS LIKE 'tbl_name';

It should be noted that the number of rows returned by the SHOW TABLE STATUS command is an approximate value and is not an accurate value in real time. This is because MySQL estimates the number of rows in some cases rather than calculating them in real time. If an exact number of rows is required, it is recommended to use the COUNT(*) function or query the information_schema.TABLES view.

12. Check the execution efficiency of query statements

EXPLAIN is a tool for query optimization that provides detailed information about the execution plan of a SELECT query. By using the EXPLAIN command, you can understand how MySQL executes the query, including the indexes used, the connection type, the number of rows scanned, etc.

{
   
   EXPLAIN | DESCRIBE | DESC} select_statement;

The output of the EXPLAIN command contains the following columns:

id:查询的标识符,用于标识查询中的每个步骤。
select_type:查询的类型,如 SIMPLE(简单查询)、PRIMARY(主查询)、SUBQUERY(子查询)等。
table:查询涉及的表。
partitions:查询涉及的分区。
type:访问表的方式,如 ALL(全表扫描)、INDEX(使用索引扫描)、RANGE(范围扫描)等。
possible_keys:可能使用的索引。
key:实际使用的索引。
key_len:使用的索引的长度。
ref:与索引比较的列或常量。
rows:扫描的行数。
filtered:过滤的行百分比。
Extra:额外的信息,如使用了临时表、使用了文件排序等。

13. View warnings during SQL execution

SHOW WARNINGS is a command used to view warning information generated by the most recently executed statement. In MySQL, a warning (Warning) is a message indicating a potential problem or abnormal situation. It will not cause the execution of the statement to fail, but may affect the query results or performance.

SHOW WARNINGS;

The output of the SHOW WARNINGS command contains the following columns:

Level:警告的级别,如 Warning、Note 等。
Code:警告的代码。
Message:警告的具体消息。

By viewing the warning information, you can learn about possible problems or exceptions during statement execution, such as data truncation, data loss, etc. Based on the warning information, corresponding adjustments and processing can be made to ensure the correctness and performance of the query.

14. View the maximum value of the auto-incremented primary key

  • Use the MAX function.
SELECT MAX(id) FROM your_table_name;
  • View table status
SHOW TABLE STATUS LIKE 'your_table_name';

In the query results, you can look for the Auto_increment column, which displays the next value of the auto-increment primary key.


references

MySQL 8.0 Reference Manual :: 13.2.13 SELECT Statement
MySQL 8.0 Reference Manual :: 13.2.18 UNION Clause
MySQL 8.0 Reference Manual :: 13.2.13.2 JOIN Clause
MySQL 8.0 Reference Manual :: 13.8.2 EXPLAIN Statement
8.8.1 Optimizing Queries with EXPLAIN

Guess you like

Origin blog.csdn.net/K346K346/article/details/132245149
Recommended