MySQL快查-子查询

MySQL快查

因为在日常工作学习中经常忘记mysql的一些语句、关键字、操作等内容,所以最近抽取时间写了以下关于mysql相关内容。相当于一本字典吧


重置mysql密码
数据类型
运算符
常用函数
数据完整性
数据库的基本操作
对表本身的操作
对表中数据的操作
本文
多表连接
索引
视图
预处理SQL语句
自定义函数与存储过程
在MySQL中编程


写在前面

子查询就是一个嵌套在select、insert、update、delete语句(或者其它子查询)中的查询。

  • 子查询常见运算:
    • [not] in
    • any
    • all
    • [not] exists
    • 逻辑运算符
  • 根据查询结果可分为:
    • 表子查询: 返回一个表
    • 行自查询:返回带有一个或多个值的一行
    • 列子查询:返回的每一行只有一列
    • 标量自查询:之返回一个值

注:

  • 子查询最多可以嵌套32层
  • 只能在子查询外层使用order by
  • 不能检索数据类型为text、blob、longtext等列

举例

例子使用的表

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)

使用的表中存在的数据

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)

当成表达式使用

# 查看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)

配合where字句使用

# 查询购买单品价格大于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)

生成子表

# 查询购买总价大于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)

猜你喜欢

转载自blog.csdn.net/weixin_45345384/article/details/116983648