MySQL database - MySQL subquery

Subquery is a common query method in MySQL, and multi-table query can be realized through subquery. Subquery refers to nesting one query statement within another query statement. Subqueries can be used in SELECT, UPDATE, and DELETE statements, and can be nested multiple levels. In actual development, subqueries often appear in the WHERE clause.

The syntax of a subquery in WHERE is as follows:

WHERE <expression> <operator> (subquery)

Among them, the operator can be a comparison operator and keywords such as IN, NOT IN, EXISTS, NOT EXISTS, etc.

1)IN | NOT IN

When the expression is equal to a value in the result set returned by the subquery, it returns TRUE, otherwise it returns FALSE; if the keyword NOT is used, the return value is just the opposite.

2)EXISTS | NOT EXISTS

It is used to judge whether the result set of the subquery is empty. If the result set of the subquery is not empty, it returns TRUE, otherwise it returns FALSE; if the keyword NOT is used, the returned value is just the opposite.

Example 1

Use a subquery to query the names of students who study Java courses in the tb_students_info table and tb_course table. The SQL statement and running results are as follows:

mysql> SELECT name FROM tb_students_info 
    -> WHERE course_id IN (SELECT id FROM tb_course WHERE course_name = 'Java');
+-------+
| name  |
+-------+
| Dany  |
| Henry |
+-------+
2 rows in set (0.01 sec)

It turns out that only Dany and Henry are taking the Java course. The above query process can also be divided into the following two steps, and the effect is the same.

1) First, execute the inner query separately to find out the id of the Java course in the tb_course table. The SQL statement and the running results are as follows:

mysql> SELECT id FROM tb_course 
    -> WHERE course_name = 'Java';
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)

As you can see, the value of the id field that meets the criteria is 1.

2) Then execute the outer query to query the name of the student whose course_id is equal to 1 in the tb_students_info table. The SQL statement and running results are as follows:

mysql> SELECT name FROM tb_students_info 
    -> WHERE course_id IN (1);
+-------+
| name  |
+-------+
| Dany  |
| Henry |
+-------+
2 rows in set (0.00 sec)

By convention, the outer SELECT query is called the parent query, and the query embedded in parentheses is called a subquery (subqueries must be placed in parentheses). When MySQL processes the SELECT statement in the above example, the execution flow is: execute the subquery first, and then execute the parent query.

Example 2

Similar to Example 1, use the NOT IN keyword in the SELECT statement to query the names of students who have not studied Java courses. The SQL statement and running results are as follows:

mysql> SELECT name FROM tb_students_info 
    -> WHERE course_id NOT IN (SELECT id FROM tb_course WHERE course_name = 'Java');
+--------+
| name   |
+--------+
| Green  |
| Jane   |
| Jim    |
| John   |
| Lily   |
| Susan  |
| Thomas |
| Tom    |
| LiMing |
+--------+
9 rows in set (0.01 sec)

It can be seen that the running result is just the opposite of Example 1, and the students who did not learn the Java course are students other than Dany and Henry.

Example 3

Use =operators to query the names of all students who study Python courses in the tb_course table and tb_students_info table. The SQL statement and running results are as follows:

mysql> SELECT name FROM tb_students_info
    -> WHERE course_id = (SELECT id FROM tb_course WHERE course_name = 'Python');
+------+
| name |
+------+
| Jane |
+------+
1 row in set (0.00 sec)

It turns out that the only student taking the Python course is Jane.

Example 4

Use <>operators to query the names of students who have not studied Python courses in the tb_course table and tb_students_info table. The SQL statement and running results are as follows:

mysql> SELECT name FROM tb_students_info
    -> WHERE course_id <> (SELECT id FROM tb_course WHERE course_name = 'Python');
+--------+
| name   |
+--------+
| Dany   |
| Green  |
| Henry  |
| Jim    |
| John   |
| Lily   |
| Susan  |
| Thomas |
| Tom    |
| LiMing |
+--------+
10 rows in set (0.00 sec)

It can be seen that the running result is just the opposite of that of Example 3, and the students who did not learn the Python course were students other than Jane.

Example 5

Query whether there is a course with id=1 in the tb_course table. If it exists, query the records in the tb_students_info table. The SQL statement and the running results are as follows:

mysql> SELECT * FROM tb_students_info
    -> WHERE EXISTS(SELECT course_name FROM tb_course WHERE id=1);
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | Jim    |   24 | 女   |    175 |         2 |
|  6 | John   |   21 | 女   |    172 |         4 |
|  7 | Lily   |   22 | 男   |    165 |         4 |
|  8 | Susan  |   23 | 男   |    170 |         5 |
|  9 | Thomas |   22 | 女   |    178 |         5 |
| 10 | Tom    |   23 | 女   |    165 |         5 |
| 11 | LiMing |   22 | 男   |    180 |         7 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.01 sec)

It can be seen from the result that there is a record with id=1 in the tb_course table, so the EXISTS expression returns TRUE, and the outer query statement queries the table tb_students_info after receiving TRUE, and returns all the records.

The EXISTS keyword can be used together with other query conditions, and the conditional expression and the EXISTS keyword are connected by AND and OR.

Example 6

Query whether there is a course with id=1 in the tb_course table. If it exists, query the records with the age field greater than 24 in the tb_students_info table. The SQL statement and the running results are as follows:

mysql> SELECT * FROM tb_students_info
    -> WHERE age>24 AND EXISTS(SELECT course_name FROM tb_course WHERE id=1);
+----+------+------+------+--------+-----------+
| id | name | age  | sex  | height | course_id |
+----+------+------+------+--------+-----------+
|  1 | Dany |   25 | 男   |    160 |         1 |
+----+------+------+------+--------+-----------+
1 row in set (0.01 sec)

The results show that a record is queried from the tb_students_info table, and the value of the age field of this record is 25. The inner query statement queries the records from the tb_course table and returns TRUE. The outer query statement starts the query. According to the query condition, query the records whose age is greater than 24 from the tb_students_info table.

expand

The function of subquery can also be accomplished by table join, but subquery will make the SQL statement easier to read and write.

Generally speaking, table joins (inner joins and outer joins, etc.) can be replaced by subqueries, but the reverse is not necessarily the case. Some subqueries cannot be replaced by table joins. Subqueries are more flexible, convenient, and have various forms, and are suitable as query filtering conditions, while table joins are more suitable for viewing data in joined tables.

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130379484