目录
多表查询
1、联合查询
在一个表中,通过联查查询的方式筛选符合不同条件的数据条。联合查询使用关键字union实现。
//查看正常查询的表内容
mysql> select * from chose;
+--------+---------+-------+
| number | subject | score |
+--------+---------+-------+
| 1 | 高数 | 98 |
| 1 | 大物 | 99 |
| 2 | 英语 | 87 |
| 3 | 高数 | 77 |
+--------+---------+-------+
4 rows in set (0.01 sec)
//查看使用联合查询的表内容
mysql> select * from chose where number=1
-> union
-> select * from chose where number=2;
+--------+---------+-------+
| number | subject | score |
+--------+---------+-------+
| 1 | 高数 | 98 |
| 1 | 大物 | 99 |
| 2 | 英语 | 87 |
+--------+---------+-------+
3 rows in set (0.00 sec)
2、连接查询
交叉查询
交叉查询是通过cross join关键字实现的。
交叉查询返回的结果是被连接的两个表的所有数据行的笛卡尔积。
mysql> select student.number,student.name,chose.subject,chose.score
-> from student
-> cross join chose;
+--------+--------+---------+-------+
| number | name | subject | score |
+--------+--------+---------+-------+
| 4 | 心心 | 高数 | 98 |
| 3 | 可可 | 高数 | 98 |
| 2 | 周周 | 高数 | 98 |
| 1 | 娜娜 | 高数 | 98 |
| 4 | 心心 | 大物 | 99 |
| 3 | 可可 | 大物 | 99 |
| 2 | 周周 | 大物 | 99 |
| 1 | 娜娜 | 大物 | 99 |
| 4 | 心心 | 英语 | 87 |
| 3 | 可可 | 英语 | 87 |
| 2 | 周周 | 英语 | 87 |
| 1 | 娜娜 | 英语 | 87 |
| 4 | 心心 | 高数 | 77 |
| 3 | 可可 | 高数 | 77 |
| 2 | 周周 | 高数 | 77 |
| 1 | 娜娜 | 高数 | 77 |
+--------+--------+---------+-------+
16 rows in set (0.00 sec)
内连接
内连接通过关键字 inner join...on实现(其中 inner可以省略)
内连接根据匹配条件返回第一个表与第二个表中匹配的全部记录
mysql> select student.name,student.number,chose.subject,chose.score
-> from student join chose//确定进行连接查询的两个数据表
-> on student.number = chose.number;//确定匹配条件
+--------+--------+---------+-------+
| name | number | subject | score |
+--------+--------+---------+-------+
| 娜娜 | 1 | 高数 | 98 |
| 娜娜 | 1 | 大物 | 99 |
| 周周 | 2 | 英语 | 87 |
| 可可 | 3 | 高数 | 77 |
+--------+--------+---------+-------+
4 rows in set (0.00 sec)
左外连接
左外连接使用关键字 left join实现
左外连接用于返回连接关键字乐翻天left join左表中的全部记录,以及右表中符合条件的记录
右外连接
右外连接使用关键字right join实现
右外连接用于返回连接关键字右表中的所有记录,以及左表中符合连接条件的记录
关于主键、外键
添加外键约束
//添加外键约束
mysql> alter table chose add foreign key(number) references student(number);
Query OK, 4 rows affected (0.05 sec)
//查看外键约束是否成功添加
mysql> desc chose;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| number | int | YES | MUL | NULL | |
| subject | char(5) | YES | | NULL | |
| score | int | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
3 rows in set (0.02 sec)
删除外键约束
//删除外键之前的表结构,有外键的数据
mysql> show create table chose;
| chose | CREATE TABLE `chose` (
`number` int DEFAULT NULL,
`subject` char(5) DEFAULT NULL,
`score` int DEFAULT NULL,
KEY `number` (`number`),
CONSTRAINT `chose_ibfk_1` FOREIGN KEY (`number`) REFERENCES `student` (`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
//删除外键命令
mysql> alter table chose drop foreign key chose_ibfk_1;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
//查看删除之后的表结构,外键被删除了
-------------------------------+
| chose | CREATE TABLE `chose` (
`number` int DEFAULT NULL,
`subject` char(5) DEFAULT NULL,
`score` int DEFAULT NULL,
KEY `number` (`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
关于多表查询的相关内容就分享到这里了,你学废了吗,下篇文章见!!