MySQL Quick Query-Multiple Table Connection

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
operators
Common functions
Data integrity
Basic operations of the database
Operations on the table itself Operations
on the data in the table Subqueries This article Index view preprocessing SQL statements Custom functions and stored procedures Programming in MySQL







write in front

For details, please visit join clause

Tables used

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)

data in 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)

inner join

inner join
The result of inner join query only contains rows that meet the conditions.

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)

outer join

left [outer] join

The results of the left [outer] join
query include rows that meet the conditions and all rows of the left table.

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

The right [outer] join
query result contains rows that meet the conditions and all rows in the left table.

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 connect

The cross join
result contains the combination (Cartesian product) of all rows in the two tables.

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)

Guess you like

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