MySQL Quick Query-Subquery

MySQL quick check

Because I often forget some mysql statements, keywords, operations, etc. in my daily work and study, I recently took some time to write the following content about mysql. It's like a dictionary


Reset mysql password
data type
operator
common functions
data integrity
basic operations of the database
operations on the table itself operations
on the data in the table
this article
multi-table connection
index
view
preprocessing SQL statements
custom functions and stored procedures
programming in MySQL


write in front

A subquery is a query nested in a select, insert, update, delete statement (or other subquery).

  • Common subquery operations:
    • [not] in
    • any
    • all
    • [not] exists
    • Logical Operators
  • According to the query results, it can be divided into:
    • Table subquery: Returns a table
    • Row self query: returns a row with one or more values
    • Column subquery: returns only one column for each row
    • Scalar self-query: returns a value

Note:

  • Subqueries can be nested up to 32 levels
  • order by can only be used outside the subquery
  • Columns with data types such as text, blob, longtext, etc. cannot be retrieved.

Example

Table used in the example

mysql> desc user;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int           | NO   | PRI | NULL    | auto_increment |
| name  | char(10)      | YES  |     | NULL    |                |
| sex   | enum('f','m') | YES  |     | NULL    |                |
+-------+---------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> desc commodity;
+---------+-----------+------+-----+--------------+----------------+
| Field   | Type      | Null | Key | Default      | Extra          |
+---------+-----------+------+-----+--------------+----------------+
| id      | int       | NO   | PRI | NULL         | auto_increment |
| price   | int       | NO   |     | NULL         |                |
| name    | char(128) | YES  |     | 匿名商品     |                |
| user_id | int       | YES  |     | NULL         |                |
+---------+-----------+------+-----+--------------+----------------+
4 rows in set (0.01 sec)

Use data that exists in the table

mysql> select * from user;
+----+--------+------+
| id | name   | sex  |
+----+--------+------+
|  1 | 铁子   | m    |
|  2 | 嘎子   | m    |
|  3 | 潘子   | m    |
|  4 | 翠花   | f    |
|  5 | 阿秀   | f    |
+----+--------+------+
5 rows in set (0.00 sec)

mysql> select * from commodity;
+----+-------+--------+---------+
| id | price | name   | user_id |
+----+-------+--------+---------+
|  1 |   100 | 手机   |       1 |
|  2 |   299 | 电脑   |       1 |
|  3 | 18990 | 假酒   |       3 |
|  4 | 18990 | 中华   |       3 |
|  5 |    18 | 中华   |       2 |
+----+-------+--------+---------+
5 rows in set (0.00 sec)

Use as an expression

# 查看id为3的user的购买数量
select name, 
	(select count(1) from commodity where user_id = 3) 购买数量
	from user
	where id = 3;
+--------+--------------+
| name   | 购买数量     |
+--------+--------------+
| 潘子   |            2 |
+--------+--------------+
1 row in set (0.00 sec)

Used with where clauses

# 查询购买单品价格大于100的用户的姓名和性别
select name, sex
	from user
	where id in (select user_id from commodity where price > 100);
+--------+------+
| name   | sex  |
+--------+------+
| 铁子   | m    |
| 潘子   | m    |
+--------+------+
2 rows in set (0.00 sec)

Generate subtable

# 查询购买总价大于1000的人
select name as 姓名,sex as 性别 
from user 
where id in 
	(select user_id 
	from (select user_id,sum(price) total from commodity group by user_id) as newTable 
	where newTable.total > 100);
+--------+--------+
| 姓名   | 性别   |
+--------+--------+
| 铁子   | m      |
| 潘子   | m      |
+--------+--------+
2 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/weixin_45345384/article/details/116983648