MySQL快查-多表连接

MySQL快查

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


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


写在前面

详情请访问join字句

用到的表

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.00 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.00 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)

内连接

inner join
通过内连接查询的结果只包含满足条件的行。

select user.name as 姓名,user.sex as 性别,commodity.name as 商品名称 
from user 
inner join commodity
on user.id = commodity.user_id;
+--------+--------+--------------+
| 姓名   | 性别   | 商品名称     |
+--------+--------+--------------+
| 铁子   | m      | 手机         |
| 铁子   | m      | 电脑         |
| 潘子   | m      | 假酒         |
| 潘子   | m      | 中华         |
| 嘎子   | m      | 中华         |
+--------+--------+--------------+
5 rows in set (0.00 sec)

# 只显示符合"user.id = commodity.user_id"条件的行
# 还可以通过其他字句进一步筛选,如:

select user.name as 姓名,user.sex as 性别,commodity.name as 商品名称 
from user 
inner join commodity
on user.id = commodity.user_id
where commodity.price > 100;
+--------+--------+--------------+
| 姓名   | 性别   | 商品名称     |
+--------+--------+--------------+
| 铁子   | m      | 电脑         |
| 潘子   | m      | 假酒         |
| 潘子   | m      | 中华         |
+--------+--------+--------------+
3 rows in set (0.00 sec)

外连接

左[外]连接

left [outer] join
查询的结果包括满足条件的行和左边的表的全部行。

select user.name as 姓名,user.sex as 性别,commodity.name as 商品名称 
from user 
left join commodity
on user.id = commodity.user_id;
+--------+--------+--------------+
| 姓名   | 性别   | 商品名称     |
+--------+--------+--------------+
| 铁子   | m      | 电脑         |
| 铁子   | m      | 手机         |
| 嘎子   | m      | 中华         |
| 潘子   | m      | 中华         |
| 潘子   | m      | 假酒         |
| 翠花   | f      | NULL         |
| 阿秀   | f      | NULL         |
+--------+--------+--------------+
7 rows in set (0.00 sec)
# “翠花”和“阿秀”不满足条件,也会显示出来

右[外]连接

right [outer] join
查询结果包含满足条件的行和左边表的所有行。

select user.name as 姓名,user.sex as 性别,commodity.name as 商品名称 
from user 
right join commodity
on user.id = commodity.user_id;
+--------+--------+--------------+
| 姓名   | 性别   | 商品名称     |
+--------+--------+--------------+
| 铁子   | m      | 手机         |
| 铁子   | m      | 电脑         |
| 潘子   | m      | 假酒         |
| 潘子   | m      | 中华         |
| 嘎子   | m      | 中华         |
+--------+--------+--------------+
5 rows in set (0.00 sec)
# 因为我的右表的全部行都满足条件,所以左表列中没有显示NULL

交叉连接

cross join
结果包含两表所有行的组合(笛卡尔积)。

select user.name as 姓名,user.id as uid,commodity.id as cid 
from user 
cross join commodity;
+--------+-----+-----+
| 姓名   | uid | cid |
+--------+-----+-----+
| 铁子   |   1 |   5 |
| 铁子   |   1 |   4 |
| 铁子   |   1 |   3 |
| 铁子   |   1 |   2 |
| 铁子   |   1 |   1 |
| 嘎子   |   2 |   5 |
| 嘎子   |   2 |   4 |
| 嘎子   |   2 |   3 |
| 嘎子   |   2 |   2 |
| 嘎子   |   2 |   1 |
| 潘子   |   3 |   5 |
| 潘子   |   3 |   4 |
| 潘子   |   3 |   3 |
| 潘子   |   3 |   2 |
| 潘子   |   3 |   1 |
| 翠花   |   4 |   5 |
| 翠花   |   4 |   4 |
| 翠花   |   4 |   3 |
| 翠花   |   4 |   2 |
| 翠花   |   4 |   1 |
| 阿秀   |   5 |   5 |
| 阿秀   |   5 |   4 |
| 阿秀   |   5 |   3 |
| 阿秀   |   5 |   2 |
| 阿秀   |   5 |   1 |
+--------+-----+-----+
25 rows in set (0.00 sec)

猜你喜欢

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