文章目录
- 一.常用查询
- 1.按关键字排序(ORDER BY 语句)
- 1.1 语法格式
- 1.2 ASC和DESC的排序概念
- 1.3 举例
- 1.3.1 数据库有一张info表,记录了学生的id,姓名,分数,地址和爱好
- 1.3.2 按分数排序,默认不指定是升序排列
- 1.3.3 分数按降序排列
- 1.3.4 order by 还可以结合where进行条件过滤,筛选地址是南京的学生按分数降序排列
- 1.3.5 多个字段来进行排序
- 1.3.5.1 查询学生信息先按兴趣id降序排列,相同分数的,id也按降序排列
- 1.3.5.2 查询学生信息先按兴趣id降序排列,相同分数的,id按升序排列
- 2.区间判断及查询不重复记录
- 2.1 区间判断使用的语句AND/OR ——且/或
- 2.1.1 举例
- 2.1.1.1 需求:查询表格中成绩大于70且小于等于90
- 2.1.1.2 需求:查询表格中成绩大于70或小于等于90
- 2.2 嵌套/多条件
- 2.2.1 举例
- 2.2.1.1 需求:查询表中成绩大于75且小于90或大于70的数据
- 2.3 distinct 查询不重复记录
- 3. 对结果进行分组(GROUP BY 语句)
- 3.1 格式
- 3.2 举例
- 3.2.1 需求:按hobbid相同的分组,计算hobbid相同分组的个数
- 3.2.2 需求:结合where语句,筛选分数大于等于80的分组,计算学生个数
- 3.2.3 需求:结合order by把计算出的学生个数按升序排列
- 4.限制结果条目(limit⭐⭐⭐)
- 4.1 语法
- 4.2 举例
- 4.2.1 查询所有信息显示前4行记录
- 4.2.2 从第4行开始,往后显示3行内容
- 4.2.3 结合order by语句,按id的大小升序排列显示前三行
- 4.2.4 基础select 小的升阶 怎么输出最后三行⭐⭐
- 5.设置别名(alias ——as)
- 5.1 格式语法
- 5.2 列别名设置示例:
- 5.3 示例
- 5.3.1 查询info表的字段数量,以number显示
- 5.3.2 使用场景:
- 5.3.3 AS 作为连接语句的操作符。
- 5.3.4 克隆、复制表结构
- 5.3.5加入where 语句判断复制表
- 6.通配符
- 6.1 通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。
- 6.2 举例
- 6.2.1 需求:查询名字是c开头的记录
- 6.2.2 查询名字里是c和i中间有一个字符的记录
- 6.2.3 查询名字中间有g的记录
- 6.2.4 查询Wang后面2个字符的名字记录
- 6.2.5 通配符“%”和“_”不仅可以单独使用,也可以组合使用,查询名字以s开头的记录
- 7.子查询
- 7.1 不同表/多表示例
- 7.1.1 多表查询
- 7.1.1.1 语法
- 7.1.1.2 示例
- 7.2 子查询 (INSERT 语句)
- 7.3 子查询 (UPDATE 语句)
- 7.3.1 子查询 (UPDATE 语句的not in)
- 7.4 子查询(DELETE语句)
- 7.4.1 需求:删除分数大于80的记录
- 7.4.2 IN 前面还可以添加 NOT
- 7.4.2.1 需求:删除分数不是大于等于80的记录
- 7.5 子查询 (EXISTS 语句)
- 7.5.1 查询如果存在分数等于80的记录则计算test2的字段数
- 7.5.2 检验如果存在分数小于50的记录则计算test2的字段数
- 7.6 子查询(别名as)
- 7.6.1 别名的应用场景
- 7.6.1.1 需求:从info表中的id和name字段的内容做为"内容" 输出id的部分
- 二.MySQL视图
- 1.MySQL视图的概念
- 2.视图作用场景
- 3.视图作用范围
- 4.视图功能
- 5. 视图和表的区别和联系
- 5.1 区别
- 5.2 联系
- 6.示例
- 6.1 满足80分的学生展示在视图中
- 6.1.1 创建视图(单表)
- 6.1.2 查看表状态
- 6.1.3 查看视图
- 6.1.4 查看视图与源表结构
- 6.2 多表创建视图
- 6.2.1 需求:需要创建一个视图,需要输出id、学生姓名、分数
- 6.3 修改原表数据
- 6.4 同时可以通过视图修改原表
- 7.NULL 值
- 7.1 插入一条记录,分数字段输入null,显示出来就是null
- 三.连接查询
- 1.模板数据
- 2.内连接
- 2.1 语法
- 2.2 示例
- 2.左连接
- 3.右连接
一.常用查询
(增、删、改、查)
对 MySQL 数据库的查询,除了基本的查询外,有时候需要对查询的结果集进行处理。
例如只取 10 条数据、对查询结果进行排序或分组等等
1.按关键字排序(ORDER BY 语句)
类比于windows 任务管理器
使用 SELECT 语句可以将需要的数据从 MySQL 数据库中查询出来,如果对查询的结果进行排序,可以使用 ORDER BY 语句来对语句实现排序,并最终将排序后的结果返回给用户。这个语句的排序不光可以针对某一个字段,也可以针对多个字段
1.1 语法格式
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ...
1.2 ASC和DESC的排序概念
ASC 是按照升序进行排序的,默认的排序方式,即 ASC 可以省略。
DESC 是按降序方式进 行排列。当然 ORDER BY 前面也可以使用 WHERE 子句对查询结果进一步过滤。
1.3 举例
1.3.1 数据库有一张info表,记录了学生的id,姓名,分数,地址和爱好
create database class;show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| class |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
use class;
create table info (id int,name varchar(10) primary key not null ,score decimal(5,2),address varchar(20),hobbid int(5));
insert into info values(1,'liuyi',80,'beijing',2);
insert into info values(2,'wangwu',90,'shengzheng',2);
insert into info values(3,'lisi',60,'shanghai',4);
insert into info values(4,'tianqi',99,'hangzhou',5);
insert into info values(5,'jiaoshou',98,'laowo',3);
insert into info values(6,'hanmeimei',10,'nanjing',3);
insert into info values(7,'lilei',11,'nanjing',5);
mysql> select * from info;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
1.3.2 按分数排序,默认不指定是升序排列
mysql> select id,name,score from info order by score;
+------+-----------+-------+
| id | name | score |
+------+-----------+-------+
| 6 | hanmeimei | 10.00 |
| 7 | lilei | 11.00 |
| 3 | lisi | 60.00 |
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 5 | jiaoshou | 98.00 |
| 4 | tianqi | 99.00 |
+------+-----------+-------+
7 rows in set (0.00 sec)
1.3.3 分数按降序排列
mysql> select id,name,score from info order by score desc;select id,name,score from info order by score desc;
+------+-----------+-------+
| id | name | score |
+------+-----------+-------+
| 4 | tianqi | 99.00 |
| 5 | jiaoshou | 98.00 |
| 2 | wangwu | 90.00 |
| 1 | liuyi | 80.00 |
| 3 | lisi | 60.00 |
| 7 | lilei | 11.00 |
| 6 | hanmeimei | 10.00 |
+------+-----------+-------+
7 rows in set (0.00 sec)
1.3.4 order by 还可以结合where进行条件过滤,筛选地址是南京的学生按分数降序排列
mysql> select * from info;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
mysql> select name,score from info where address='nanjing' order by score desc;
+-----------+-------+
| name | score |
+-----------+-------+
| lilei | 11.00 |
| hanmeimei | 10.00 |
+-----------+-------+
2 rows in set (0.00 sec)
1.3.5 多个字段来进行排序
ORDER BY 语句也可以使用多个字段来进行排序,当排序的第一个字段相同的记录有多条的情况下,这些多条的记录再按照第二个字段进行排序,ORDER BY 后面跟多个字段时,字段之间使用英文逗号隔开,优先级是按先后顺序而定
但order by 之后的第一个参数只有在出现相同值时,第二个字段才有意义
1.3.5.1 查询学生信息先按兴趣id降序排列,相同分数的,id也按降序排列
mysql> select id,name,hobbid from info order by hobbid desc,id desc;
+------+-----------+--------+
| id | name | hobbid |
+------+-----------+--------+
| 7 | lilei | 5 |
| 4 | tianqi | 5 |
| 3 | lisi | 4 |
| 6 | hanmeimei | 3 |
| 5 | jiaoshou | 3 |
| 2 | wangwu | 2 |
| 1 | liuyi | 2 |
+------+-----------+--------+
7 rows in set (0.00 sec)
1.3.5.2 查询学生信息先按兴趣id降序排列,相同分数的,id按升序排列
mysql> select id,name,hobbid from info order by hobbid desc,id;
+------+-----------+--------+
| id | name | hobbid |
+------+-----------+--------+
| 4 | tianqi | 5 |
| 7 | lilei | 5 |
| 3 | lisi | 4 |
| 5 | jiaoshou | 3 |
| 6 | hanmeimei | 3 |
| 1 | liuyi | 2 |
| 2 | wangwu | 2 |
+------+-----------+--------+
7 rows in set (0.00 sec)
2.区间判断及查询不重复记录
2.1 区间判断使用的语句AND/OR ——且/或
2.1.1 举例
2.1.1.1 需求:查询表格中成绩大于70且小于等于90
(1)格式
elect * from 表名 where 条件判断语句
(2)示例
mysql> select * from info where score >70 and score <=90;
+------+--------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+--------+-------+------------+--------+
2 rows in set (0.00 sec)
2.1.1.2 需求:查询表格中成绩大于70或小于等于90
mysql> select * from info where score >70 or score <=90;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
2.2 嵌套/多条件
2.2.1 举例
2.2.1.1 需求:查询表中成绩大于75且小于90或大于70的数据
mysql> select * from info where score >70 or (score >75 and score <90);
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)
查询表中成绩大于75且小于90或大于70的数据id排序
mysql> select * from info where score >70 or (score >75 and score <90) order by id;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)
2.3 distinct 查询不重复记录
(1)格式:
select distinct 字段 from 表名﹔
(2)示例
mysql> select distinct hobbid from info;
+--------+
| hobbid |
+--------+
| 3 |
| 5 |
| 4 |
| 2 |
+--------+
3. 对结果进行分组(GROUP BY 语句)
通过 SQL 查询出来的结果,对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理。
3.1 格式
SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator value GROUP BY column_name;
3.2 举例
3.2.1 需求:按hobbid相同的分组,计算hobbid相同分组的个数
mysql> select count(name),hobbid from info group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 2 | 3 |
| 1 | 4 |
| 2 | 5 |
+-------------+--------+
4 rows in set (0.00 sec)
3.2.2 需求:结合where语句,筛选分数大于等于80的分组,计算学生个数
mysql> select count(name),hobbid from info where score >=80 group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 1 | 3 |
| 1 | 5 |
+-------------+--------+
3 rows in set (0.00 sec)
全班同学成绩表
count(name):计数
score 分数 :
score>=80 :优秀
score >=60 and score <80 :优-
3.2.3 需求:结合order by把计算出的学生个数按升序排列
mysql> select count(name),score,hobbid from info where score >=80 group by hobbid order by count(name) asc;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
| 1 | 98.00 | 3 |
| 1 | 99.00 | 5 |
| 2 | 80.00 | 2 |
+-------------+-------+--------+
3 rows in set (0.00 sec)
4.限制结果条目(limit⭐⭐⭐)
limit 限制输出的结果记录
在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录(行)。
有时候仅 需要返回第一行或者前几行,这时候就需要用到limit子句
4.1 语法
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。 如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的 位置偏移量是 0,第二条是 1,以此类推。第二个参数是设置返回记录行的最大数目。
4.2 举例
4.2.1 查询所有信息显示前4行记录
mysql> select * from info limit 4;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
+------+-----------+-------+----------+--------+
4 rows in set (0.00 sec)
4.2.2 从第4行开始,往后显示3行内容
mysql> select * from info limit 4,2;
+------+--------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+----------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+--------+-------+----------+--------+
2 rows in set (0.00 sec)
4.2.3 结合order by语句,按id的大小升序排列显示前三行
mysql> select id,name from info order by id limit 3;
+------+--------+
| id | name |
+------+--------+
| 1 | liuyi |
| 2 | wangwu |
| 3 | lisi |
+------+--------+
3 rows in set (0.00 sec)
4.2.4 基础select 小的升阶 怎么输出最后三行⭐⭐
mysql> select id,name from info order by id desc limit 3;
+------+-----------+
| id | name |
+------+-----------+
| 7 | lilei |
| 6 | hanmeimei |
| 5 | jiaoshou |
+------+-----------+
3 rows in set (0.01 sec)
输出前三行,怎么输出 : limit 3
limit 2 说的是前三行,limit 是做为位置偏移量的定义,他的起始是从0开始,而0表示的是字段
5.设置别名(alias ——as)
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性
5.1 格式语法
对于列的别名:SELECT column_name AS alias_name FROM table_name;
对于表的别名:SELECT column_name(s) FROM table_name AS alias_name;
在使用 AS 后,可以用 alias_name 代替 table_name,其中 AS 语句是可选的。AS 之后的别名,主要是为表内的列或者表提供临时的名称,在查询过程中使用,库内实际的表名 或字段名是不会被改变的
5.2 列别名设置示例:
select name as 姓名,score as 成绩 from info;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| hanmeimei | 10.00 |
| jiaoshou | 98.00 |
| lilei | 11.00 |
| lisi | 60.00 |
| liuyi | 80.00 |
| tianqi | 99.00 |
| wangwu | 90.00 |
+-----------+--------+
7 rows in set (0.00 sec)
如果表的长度比较长,可以使用 AS 给表设置别名,在查询的过程中直接使用别名,临时设置info的别名为i
select i.name as 姓名,i.score as 成绩 from info as i;
5.3 示例
5.3.1 查询info表的字段数量,以number显示
mysql> select count(*) as number from info;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
不用as也可以,一样显示
mysql> select count(*) number from info;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
5.3.2 使用场景:
(1)对复杂的表进行查询的时候,别名可以缩短查询语句的长度
(2)多表相连查询的时候(通俗易懂、减短sql语句)
5.3.3 AS 作为连接语句的操作符。
创建t1表,将info表的查询记录全部插入t1表
mysql> create table t1 as select * from info;
mysql> select * from t1;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
注:此处AS起到的作用:
创建了一个新表t1 并定义表结构,插入表数据(与info表相同)
但是”约束“没有被完全”复制“过来 #但是如果原表设置了主键,那么附表的:default字段会默认设置一个0
5.3.4 克隆、复制表结构
mysql> create table test2 (select * from info);
mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| info |
| t1 |
| test1 |
| test2 |
+-----------------+
4 rows in set (0.00 sec)mysql> select * from test2;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
5.3.5加入where 语句判断复制表
create table test1 as select * from info where score >=60;
show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| info |
| t1 |
| test1 |
+-----------------+
3 rows in set (0.00 sec)select * from test1;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
5 rows in set (0.00 sec)
在为表设置别名时,要保证别名不能与数据库中的其他表的名称冲突。
列的别名是在结果中有显示的,而表的别名在结果中没有显示,只在执行查询时使用。
6.通配符
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。
6.1 通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。
常用的通配符有两个,分别是:
%:百分号表示零个、一个或多个字符 *
_:下划线表示单个字符
6.2 举例
6.2.1 需求:查询名字是c开头的记录
mysql> select id,name from info where name like 'l%';
+------+-------+
| id | name |
+------+-------+
| 7 | lilei |
| 3 | lisi |
| 1 | liuyi |
+------+-------+
3 rows in set (0.00 sec)
6.2.2 查询名字里是c和i中间有一个字符的记录
mysql> select id,name from info where name like 'l_s_';
+------+------+
| id | name |
+------+------+
| 3 | lisi |
+------+------+
1 row in set (0.00 sec)
6.2.3 查询名字中间有g的记录
mysql> select id,address from info where address like '%i%';
+------+----------+
| id | address |
+------+----------+
| 6 | nanjing |
| 7 | nanjing |
| 3 | shanghai |
| 1 | beijing |
+------+----------+
4 rows in set (0.00 sec)
select id,address from info where address like '%i%' order by id;;
+------+----------+
| id | address |
+------+----------+
| 1 | beijing |
| 3 | shanghai |
| 6 | nanjing |
| 7 | nanjing |
+------+----------+
4 rows in set (0.00 sec)
6.2.4 查询Wang后面2个字符的名字记录
mysql> select id,name from info where name like 'wang__';
+------+--------+
| id | name |
+------+--------+
| 2 | wangwu |
+------+--------+
1 row in set (0.00 sec)
6.2.5 通配符“%”和“_”不仅可以单独使用,也可以组合使用,查询名字以s开头的记录
select id,name from info where name like 'l%_';
+------+-------+
| id | name |
+------+-------+
| 7 | lilei |
| 3 | lisi |
| 1 | liuyi |
+------+-------+
3 rows in set (0.00 sec)
7.子查询
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语句。
子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
PS: 子语句可以与主语句所查询的表相同,也可以是不相同
示例:
select name,score from info where id in (select id from info where score >80);
以上同表示例:
主语句:select name,score from info where id
子语句(集合): select id from info where score >80
注:子语句中的sql语句是为了,最后过滤出一个结果集,用于主语句的判断条件
in: 将主表和子表关联/连接的语法
7.1 不同表/多表示例
mysql> create table info(id int);
ERROR 1050 (42S01): Table 'info' already exists
mysql> create table tree(id int);
Query OK, 0 rows affected (0.00 sec)mysql> insert into tree values(1),(2),(3);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> select * from tree;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
7.1.1 多表查询
使用表info和tree相同的id查询出来
mysql> select id,name,score from info where id in (select * from tree);
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 3 | lisi | 60.00 |
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
3 rows in set (0.01 sec)mysql> select * from info;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE 中也同样适用。
在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套。
7.1.1.1 语法
IN 用来判断某个值是否在给定的结果集中,通常结合子查询来使用
<表达式> [NOT] IN <子查询>
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。
若启用了 NOT 关键字,则返回值相反。
需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的
7.1.1.2 示例
(1)查询分数大于80的记录
mysql> select * from info;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)mysql> select id from info where score>80;
+------+
| id |
+------+
| 5 |
| 4 |
| 2 |
+------+
3 rows in set (0.00 sec)mysql> select name,score from info where id in (select id from info where score>80);
+----------+-------+
| name | score |
+----------+-------+
| jiaoshou | 98.00 |
| tianqi | 99.00 |
| wangwu | 90.00 |
+----------+-------+
3 rows in set (0.00 sec)
7.2 子查询 (INSERT 语句)
子查询还可以用在 INSERT 语句中。子查询的结果集可以通过 INSERT 语句插入到其 他的表中
注:创建时表必须已存在,并且表的格式要和复制的表的格式相同
mysql> insert into t1 select * from info where id in (select id from info);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0mysql> select * from t1;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
7.3 子查询 (UPDATE 语句)
UPDATE 语句也可以使用子查询。UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
mysql> select * from tree;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
#此处是查出tree中的id号对应将info中的id修改
mysql> update info set score=50 where id in (select * from tree where id=2);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from info;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
7.3.1 子查询 (UPDATE 语句的not in)
表示 先匹配出tree表内的id字段为基础匹配的结果集(2,3)
然后再执行主语句,以主语句的id 为基础 进行where 条件判断/过滤
mysql> select * from tree;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)mysql> update info set score=100 where id not in (select * from tree where id >1);
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0mysql> select * from info;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 6 | hanmeimei | 100.00 | nanjing | 3 |
| 5 | jiaoshou | 100.00 | laowo | 3 |
| 7 | lilei | 100.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 100.00 | beijing | 2 |
| 4 | tianqi | 100.00 | hangzhou | 5 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
7.4 子查询(DELETE语句)
*注:在删除之前一定要确定好删除的部分,再三确认
7.4.1 需求:删除分数大于80的记录
mysql> select * from info;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 6 | hanmeimei | 100.00 | nanjing | 3 |
| 5 | jiaoshou | 100.00 | laowo | 3 |
| 7 | lilei | 100.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 100.00 | beijing | 2 |
| 4 | tianqi | 100.00 | hangzhou | 5 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)mysql> delete from info where id in (select id where score>80);
Query OK, 5 rows affected (0.00 sec)mysql> select * from info;
+------+--------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+------------+--------+
| 3 | lisi | 60.00 | shanghai | 4 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+--------+-------+------------+--------+
2 rows in set (0.00 sec)
7.4.2 IN 前面还可以添加 NOT
在 IN 前面还可以添加 NOT,其作用与IN相反,表示否定(即不在子查询的结果集里面)
7.4.2.1 需求:删除分数不是大于等于80的记录
mysql> select * from t1;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)mysql> delete from t1 where id not in (select id where score>=80);
Query OK, 3 rows affected (0.00 sec)mysql> select id,name,score from t1;
+------+----------+-------+
| id | name | score |
+------+----------+-------+
| 5 | jiaoshou | 98.00 |
| 1 | liuyi | 80.00 |
| 4 | tianqi | 99.00 |
| 2 | wangwu | 90.00 |
+------+----------+-------+
4 rows in set (0.00 sec)
7.5 子查询 (EXISTS 语句)
EXISTS 这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
7.5.1 查询如果存在分数等于80的记录则计算test2的字段数
mysql> select * from test2;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)mysql> select count(*) from tree where exists(select id from test2 where score=80);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
7.5.2 检验如果存在分数小于50的记录则计算test2的字段数
如果有的话输出所有数的字段数,没有则返回0
mysql> select * from test2;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
#有小于50的值返回所有字段数
mysql> select count(*) from test2 where exists(select id from test2 where score<50);
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
#没有大于110的值返回0
mysql> select count(*) from test2 where exists(select id from test2 where score>110);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
7.6 子查询(别名as)
#查询info表id,name 字段
select id,name from info;
以上命令可以查看到info表的内容
7.6.1 别名的应用场景
将结果集做为一张表进行查询的时候,我们也需要用到别名
7.6.1.1 需求:从info表中的id和name字段的内容做为"内容" 输出id的部分
mysql> select id from (select id,name from info);
ERROR 1248 (42000): Every derived table must have its own alias
#此时会报错,原因为:
select * from 表名 此为标准格式,而以上的查询语句,“表名"的位置其实是一个完整结果集,mysql并不能直接识别,而此时给与结果集设置一个别名,以”select a.id from a“的方式查询将此结果集视为一张"表”,就可以正常查询数据了,如下
做别名格式:select 表.字段,字段 from 表;
mysql> select id from (select id,name from info);
ERROR 1248 (42000): Every derived table must have its own alias
mysql> select a.id from (select id,name from info) a;
+------+
| id |
+------+
| 3 |
| 2 |
+------+
2 rows in set (0.00 sec)mysql> select info.id,name from info;
+------+--------+
| id | name |
+------+--------+
| 3 | lisi |
| 2 | wangwu |
+------+--------+
2 rows in set (0.00 sec)
二.MySQL视图
1.MySQL视图的概念
数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射
视图可以理解映射出来虚拟的,动态保存结果集(数据)
基础表——映射(投影)=视图
2.视图作用场景
针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示)
3.视图作用范围
select * from info; #展示的部分是info表
select * from view_name; #展示的一张或多张表
4.视图功能
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性
本质而言视图是一种select(结果集的呈现)
注:视图适合于多表连接浏览时使用!不适合增、删、改
而存储过程适合于使用较频繁的SQL语句,这样可以提高执行效率!
5. 视图和表的区别和联系
5.1 区别
①、视图是已经编译好的sql语句。而表不是
②、视图没有实际的物理记录。而表有。
show table status\G
③、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改
④、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
⑤、表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
⑥、视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的)
5.2 联系
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
6.示例
6.1 满足80分的学生展示在视图中
注:这个结果会动态变化,同时可以给不同的人群(例如权限范围)展示不同的视图
6.1.1 创建视图(单表)
create view v_score as select * from test2 where score>=80;
Query OK, 0 rows affected (0.00 sec)
6.1.2 查看表状态
show table status\G
6.1.3 查看视图
mysql> select * from v_score;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)
6.1.4 查看视图与源表结构
mysql> desc v_score;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)mysql> desc test2;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
6.2 多表创建视图
创建test03表
create table test03 (id int,name varchar(10),age char(10));
insert into test03 values(1,'zhangsan',20);
insert into test03 values(2,'lisi',30);
insert into test03 values(3,'wangwu',29);
mysql> select * from test03;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
+------+----------+------+
3 rows in set (0.00 sec)
6.2.1 需求:需要创建一个视图,需要输出id、学生姓名、分数
mysql> select * from test03;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
+------+----------+------+
3 rows in set (0.00 sec)create view v_test2(id,name,score) as select test2.id,test2.name,test2.score from test2,test03 where test2.name=test03.name;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from v_test2;
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 3 | lisi | 60.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
2 rows in set (0.00 sec)
6.3 修改原表数据
mysql> select * from test2;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)mysql> select * from v_test2;
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 3 | lisi | 60.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
2 rows in set (0.00 sec)
修改原表
update test2 set score='100' where name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
原表与视图变化
mysql> select * from test2;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 100.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)mysql> select * from v_test2;
+------+--------+--------+
| id | name | score |
+------+--------+--------+
| 3 | lisi | 100.00 |
| 2 | wangwu | 90.00 |
+------+--------+--------+
2 rows in set (0.00 sec)
6.4 同时可以通过视图修改原表
update v_info set score='120' where name='wangwu';
mysql> select * from v_info;
+------+--------+--------+
| id | name | score |
+------+--------+--------+
| 3 | lisi | 80.00 |
| 2 | wangwu | 120.00 |
+------+--------+--------+
2 rows in set (0.00 sec)mysql> select * from info;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 80.00 | shanghai | 4 |
| 1 | liuyi | 60.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 120.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
修改表不能修改以函数、复合函数方式计算出来的字段
作用:查询方便、安全性
查询方便:索引速度快、同时可以多表查询更为迅速(视图不保存真实数据,视图本质类似select)
安全性:我们实现登陆的账户是root——所拥有权限 ,视图无法显示完整的约束
7.NULL 值
在 SQL 语句使用过程中,经常会碰到 NULL 这几个字符。
通常使用 NULL 来表示缺失 的值,也就是在表中该字段是没有值的。
如果在创建表时,限制某些字段不为空,则可以使用 NOT NULL 关键字,不使用则默认可以为空。
在向表内插入记录或者更新记录时,如果该字段没有 NOT NULL 并且没有值,这时候新记录的该字段将被保存为 NULL。
需要注意 的是,NULL 值与数字 0 或者空白(spaces)的字段是不同的,值为 NULL 的字段是没有 值的。在 SQL 语句中,使用 IS NULL 可以判断表内的某个字段是不是 NULL 值,相反的用 IS NOT NULL 可以判断不是 NULL 值。
查询info表结构,name字段是不允许空值的
null值与空值的区别(空气与真空)
空值长度为0,不占空间,NULL值的长度为null,占用空间
is null无法判断空值
空值使用"=“或者”<>"来处理(!=)
count()计算时,NULL会忽略,空值会加入计算
mysql> desc info;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
7.1 插入一条记录,分数字段输入null,显示出来就是null
验证:
alter table info add column addr varchar(50);update info set addr='nj' where score >=70;
统计数量:检测null是否会加入统计中
mysql> select count(addr) from info;
+-------------+
| count(addr) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
将info表中其中一条数据修改为空值’’
mysql> update info set addr='' where name='wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from info;
+------+-----------+--------+------------+--------+------+
| id | name | score | address | hobbid | addr |
+------+-----------+--------+------------+--------+------+
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL |
| 5 | jiaoshou | 98.00 | laowo | 3 | nj |
| 7 | lilei | 11.00 | nanjing | 5 | NULL |
| 1 | liuyi | 60.00 | beijing | 2 | NULL |
| 4 | tianqi | 99.00 | hangzhou | 5 | nj |
| 2 | wangwu | 120.00 | shengzheng | 2 | |
| 3 | zjf | 80.00 | shanghai | 4 | nj |
+------+-----------+--------+------------+--------+------+
7 rows in set (0.00 sec)
统计数量,检测空值是不会被添加到统计中
mysql> select count(addr) from info;
+-------------+
| count(addr) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
查询null值
mysql> select * from info where addr is NULL;
+------+-----------+-------+---------+--------+------+
| id | name | score | address | hobbid | addr |
+------+-----------+-------+---------+--------+------+
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL |
| 7 | lilei | 11.00 | nanjing | 5 | NULL |
| 1 | liuyi | 60.00 | beijing | 2 | NULL |
+------+-----------+-------+---------+--------+------+
3 rows in set (0.00 sec)
查询不为空的值
mysql> select * from info where addr is not null;
±-----±---------±-------±-----------±-------±-----+
| id | name | score | address | hobbid | addr |
±-----±---------±-------±-----------±-------±-----+
| 5 | jiaoshou | 98.00 | laowo | 3 | nj |
| 4 | tianqi | 99.00 | hangzhou | 5 | nj |
| 2 | wangwu | 120.00 | shengzheng | 2 | |
| 3 | zjf | 80.00 | shanghai | 4 | nj |
±-----±---------±-------±-----------±-------±-----+
4 rows in set (0.00 sec)
三.连接查询
MySQL 的连接查询,通常都是将来自两个或多个表的记录行结合起来,基于这些表之间的 共同字段,进行数据的拼接。首先,要确定一个主表作为结果集,然后将其他表的行有选择 性的连接到选定的主表结果集上。使用较多的连接查询包括:内连接、左连接和右连接
1.模板数据
create table test1 (
a_id int(11) default null,
a_name varchar(32) default null,
a_level int(11) default null);create table test2 (
b_id int(11) default null,
b_name varchar(32) default null,
b_level int(11) default null);insert into test1 values (1,'aaaa',10);
insert into test1 values (2,'bbbb',20);
insert into test1 values (3,'cccc',30);
insert into test1 values (4,'dddd',40);insert into test2 values (2,'bbbb',20);
insert into test2 values (3,'cccc',30);
insert into test2 values (5,'eeee',50);
insert into test2 values (6,'ffff',60);
2.内连接
输出匹配共同数据/共同字段数据内容
MySQL 中的内连接就是两张或多张表中同时符合某种条件的数据记录的组合。通常在 FROM 子句中使用关键字 INNER JOIN 来连接多张表,并使用 ON 子句设置连接条件,内连接是系统默认的表连接,所以在 FROM 子句后可以省略 INNER 关键字,只使用 关键字 JOIN。同时有多个表时,也可以连续使用 INNER JOIN 来实现多表的内连接,不过为了更好的性能,建议最好不要超过三个表。
2.1 语法
SELECT column_name(s)FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
2.2 示例
insert into infos values('wangwu',80,'beijing'),('zhangsan',99,'shanghai'),('lisi',90,'nanjing'););
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from infos;
+----------+-------+----------+
| name | score | address |
+----------+-------+----------+
| wangwu | 80.00 | beijing |
| zhangsan | 99.00 | shanghai |
| lisi | 90.00 | nanjing |
+----------+-------+----------+
3 rows in set (0.00 sec)
mysql> select info.id,info.name from info inner join infos on info.name=infos.name;
+------+--------+
| id | name |
+------+--------+
| 2 | wangwu |
+------+--------+
1 row in set (0.00 sec)
内连查询:通过inner join 的方式将两张表指定的相同字段的记录行输出出来
内连查询:
用inner join 就可以
2.左连接
将两张表的内容进行匹配按照sql查询的顺序(左——右)输出左表的全部内容和右表的共同数据的内容(以左表为主)
左连接也可以被称为左外连接,在 FROM 子句中使用 LEFT JOIN 或者 LEFT OUTER JOIN 关键字来表示。
mysql> select * from info left join infos on info.name=infos.name;
+------+-----------+--------+------------+--------+------+--------+-------+---------+
| id | name | score | address | hobbid | addr | name | score | address |
+------+-----------+--------+------------+--------+------+--------+-------+---------+
| 2 | wangwu | 120.00 | shengzheng | 2 | | wangwu | 80.00 | beijing |
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL | NULL | NULL | NULL |
| 5 | jiaoshou | 98.00 | laowo | 3 | nj | NULL | NULL | NULL |
| 7 | lilei | 11.00 | nanjing | 5 | NULL | NULL | NULL | NULL |
| 1 | liuyi | 60.00 | beijing | 2 | NULL | NULL | NULL | NULL |
| 4 | tianqi | 99.00 | hangzhou | 5 | nj | NULL | NULL | NULL |
| 3 | zjf | 80.00 | shanghai | 4 | nj | NULL | NULL | NULL |
+------+-----------+--------+------------+--------+------+--------+-------+---------+
7 rows in set (0.00 sec)
左连接中左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录,右表记录不足的地方均为 NULL。
3.右连接
将两张表的内容进行匹配按照sql查询的顺序(右——左)输出右表的全部内容和左表的共同数据的内容(以右表为主)
右连接也被称为右外连接,在 FROM 子句中使用 RIGHT JOIN 或者 RIGHT OUTER JOIN 关键字来表示。
mysql> select * from info right join infos on info.name=infos.name;
+------+--------+--------+------------+--------+------+----------+-------+----------+
| id | name | score | address | hobbid | addr | name | score | address |
+------+--------+--------+------------+--------+------+----------+-------+----------+
| 2 | wangwu | 120.00 | shengzheng | 2 | | wangwu | 80.00 | beijing |
| NULL | NULL | NULL | NULL | NULL | NULL | zhangsan | 99.00 | shanghai |
| NULL | NULL | NULL | NULL | NULL | NULL | lisi | 90.00 | nanjing |
+------+--------+--------+------------+--------+------+----------+-------+----------+
3 rows in set (0.00 sec)
在右连接的查询结果集中,除了符合匹配规则的行外,还包括右表中有但是左表中不匹 配的行,这些记录在左表中以 NULL。