作者:周棋洛
上篇:三篇学会MySQL数据库,第一篇
简介:MySQL万字教程,从入门到精通,第二篇查询,第三篇讲讲解视图,存储过程,触发器,事物,维护,备份恢复等高级知识,单独写一篇是因为查询使用频率太高,一万五千字,放上完美无水印脑图,方便下载后期复习,原创不易,一键三联,多多支持呀!⛽
mysql> desc stus;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| gradeId | int(11) | YES | MUL | NULL | |
| NAME | varchar(100) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
| bir | date | YES | | NULL | |
| english | int(3) | YES | | NULL | |
| chinese | int(3) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+--------------+------+------------+---------+---------+
select 列名 from 表;
从stus表中查询名字的字段
mysql> select name from stus;
+--------------+
| name |
+--------------+
| 周棋洛 |
| 张郁苗 |
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
select 列名,列名…… from 表;
从stus表中查询名字和年龄这两个字段
mysql> select name,age from stus;
+--------------+------+
| name | age |
+--------------+------+
| 周棋洛 | 18 |
| 张郁苗 | 18 |
| 小猪佩奇 | 6 |
| 猪爸爸 | 8 |
| 猪妈妈 | 7 |
+--------------+------+
select * from 表;
从stus表查询所有字段
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+--------------+------+------------+---------+---------+
distinct关键字可以出去重复出现的内容,注意:不能部分使用distinct
mysql> select distinct age from stus;
+------+
| age |
+------+
| 18 |
| 6 |
| 8 |
| 7 |
+------+
我们在查询时,如果对原有字段名不满意,这时就可以通过as关键字对字段起别名,注意:as是可省略的,如下所示
mysql> select name as "姓名" from stus;
+--------------+
| 姓名 |
+--------------+
| 周棋洛 |
| 张郁苗 |
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
mysql> select name "姓名" from stus;
+--------------+
| 姓名 |
+--------------+
| 周棋洛 |
| 张郁苗 |
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
: 查找stus表中年龄大于18岁的学生姓名
mysql> select name from stus where age > 18;
Empty set (0.00 sec)
: 查找stus表中年龄小于18岁的学生姓名
mysql> select name from stus where age < 18;
+--------------+
| name |
+--------------+
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
: 查找stus表中年龄等于18岁的学生姓名
mysql> select name from stus where age = 18;
+-----------+
| name |
+-----------+
| 周棋洛 |
| 张郁苗 |
+-----------+
: 查找stus表中年龄大于等于18岁的学生姓名
mysql> select name from stus where age >= 18;
+-----------+
| name |
+-----------+
| 周棋洛 |
| 张郁苗 |
+-----------+
: 查找stus表中年龄小于等于18岁的学生姓名
mysql> select name from stus where age <= 18;
+--------------+
| name |
+--------------+
| 周棋洛 |
| 张郁苗 |
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
: 查找stus表中年龄不等于18岁的学生姓名
mysql> select name from stus where age <> 18;
+--------------+
| name |
+--------------+
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
mysql> select name from stus where age != 18;
+--------------+
| name |
+--------------+
| 小猪佩奇 |
| 猪爸爸 |
| 猪妈妈 |
+--------------+
: 查找stus表中年龄在10~18岁的学生姓名
mysql> select name,age from stus where age between 8 and 20;
+-----------+------+
| name | age |
+-----------+------+
| 周棋洛 | 18 |
| 张郁苗 | 18 |
| 猪爸爸 | 8 |
+-----------+------+
: 查找stus表中年龄为8岁或7岁的学生姓名
mysql> select name,age from stus where age in(7,8);
+-----------+------+
| name | age |
+-----------+------+
| 猪爸爸 | 8 |
| 猪妈妈 | 7 |
+-----------+------+
: 查找stus表中名字为空的学生信息
mysql> select * from stus where name is null;
Empty set (0.02 sec)
: 查找stus表中名字不为空的学生信息
mysql> select * from stus where name is not null;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+--------------+------+------------+---------+---------+
只有多个条件都成立才会返回真,否则就认为false
: 查找stus表中年龄为18岁的周棋洛的学生信息
mysql> select * from stus where name = "周棋洛" AND age = "18";
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
+----+---------+-----------+------+------------+---------+---------+
mysql> select * from stus where name = "周棋洛" && age = "18";
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
+----+---------+-----------+------+------------+---------+---------+
只要有一个条件成立就返回真,都不成立返回false
: 查找stus表中名字叫周棋洛或者叫张郁苗的学生信息
mysql> select * from stus where name = "周棋洛" OR name = "张郁苗";
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
+----+---------+-----------+------+------------+---------+---------+
mysql> select * from stus where name = "周棋洛" || name = "张郁苗";
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
+----+---------+-----------+------+------------+---------+---------+
糊查询
需要使用 like
占位符,几个案例学会模糊查询,下面是stus表的数据
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+--------------+------+------------+---------+---------+
1.查询 stus 表中 name 以猪开头的学生信息
mysql> select * from stus where name like "猪%";
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+-----------+------+------------+---------+---------+
2.查询 stus 表中 name 以爸爸结尾的学生信息
mysql> select * from stus where name like '%爸爸';
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
+----+---------+-----------+------+------------+---------+---------+
3.查询 stus 表中 name 中包含猪的学生信息
mysql> select * from stus where name like '%猪%';
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+--------------+------+------------+---------+---------+
4.查询 stus 表中 name 以猪开头的且名字长度为2的学生信息
mysql> select * from stus where name like '猪_';
Empty set (0.00 sec)
5.查询 stus 表中 name 以猪开头的且名字长度为3的学生信息
mysql> select * from stus where name like '猪__';
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+-----------+------+------------+---------+---------+
6.查询 stus 表中 name 以妈妈结尾的且名字长度为3的学生信息
mysql> select * from stus where name like '_妈妈';
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+-----------+------+------------+---------+---------+
7.查询 stus 表中 name 以张开头以苗结尾的且名字长度为3的学生信息
mysql> select * from stus where name like '张_苗';
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
+----+---------+-----------+------+------------+---------+---------+
关键字:order by
关系型数据库设计理论认为,如果不明确规定排序顺序,则不应该假定检索出来的数据的顺序有意义,可以使用 order by 子句
对查询结果进行排序,默认是升序
,即从小到大 ASC
,如果想要降序
排序,则需要指定 DESC
1. 查询stus表中名字字段,要求按照年龄的升序进行排序
mysql> select name from stus order by age;
+--------------+
| name |
+--------------+
| 小猪佩奇 |
| 猪妈妈 |
| 猪爸爸 |
| 周棋洛 |
| 张郁苗 |
+--------------+
2. 查询stus表中名字,年龄字段,要求按照年龄大于7进行筛选之后按照年龄的升序进行排序
mysql> select name,age from stus where age > 7 order by age;
+-----------+------+
| name | age |
+-----------+------+
| 猪爸爸 | 8 |
| 周棋洛 | 18 |
| 张郁苗 | 18 |
+-----------+------+
3. 查询stus表中名字字段,要求按照年龄的升序进行排序,如果年龄相同再按照英语成绩降序排列
mysql> select name from stus order by age,english desc;
+--------------+
| name |
+--------------+
| 小猪佩奇 |
| 猪妈妈 |
| 猪爸爸 |
| 张郁苗 |
| 周棋洛 |
+--------------+
这个还没讲到,如果你不知道,可以跳过,后面会将到哦
4. 查询stus表中名字字段,要求按照年龄的升序进行排序,并通过limit返回一条数据,即年龄最小的
mysql> select name from stus order by age asc limit 1;
+--------------+
| name |
+--------------+
| 小猪佩奇 |
+--------------+
哦,原来小猪佩奇是最小的
在使用 order by 子句对检索出的数据进行排序时,应该保证它是位于 from,如果有条件,应位于where之后,如果使用 limit ,它必须位于 order by 之后,使用子句的次序不对将产生错误消息
MySQL 提供了5个聚合函数,聚合函数能够汇总数据,这些函数是高效设计的,它们返回结果一般比你自己在客户机应用程序中计算要快的多
返回某列的平均值
查看stus表学生的平均英语成绩
mysql> select avg(english) as avgEnglish from stus;
+------------+
| avgEnglish |
+------------+
| 66.4000 |
+------------+
返回某列的行数
查看stus表一共有多少行
mysql> select count(*) as num from stus;
+-----+
| num |
+-----+
| 5 |
+-----+
返回某列的最大值
查看stus表中英语成绩最高分
mysql> select max(english) as score from stus;
+-------+
| score |
+-------+
| 130 |
+-------+
返回某列的最小值
查询stus表英语成绩的最低分
mysql> select min(english) as score from stus;
+-------+
| score |
+-------+
| 34 |
+-------+
还不错,没有0蛋
返回某列之和
查询stus表中学生英语成绩的总分
mysql> select sum(english) as score from stus;
+-------+
| score |
+-------+
| 332 |
+-------+
对stus表的数据按照班级进行分组,并查看每个班都有多少人
mysql> select gradeId,count(*) as num from stus group by gradeId;
+---------+-----+
| gradeId | num |
+---------+-----+
| 1 | 2 |
| 2 | 1 |
| 3 | 2 |
+---------+-----+
对stus表的数据按照班级进行分组,查看每个班都有多少人以及班级英语平均分
mysql> select gradeId,avg(english)as english from stus group by gradeId;
+---------+----------+
| gradeId | english |
+---------+----------+
| 1 | 56.0000 |
| 2 | 130.0000 |
| 3 | 45.0000 |
+---------+----------+
mysql> select gradeId,count(*) as num from stus group by gradeId having count(*) > 1;
+---------+-----+
| gradeId | num |
+---------+-----+
| 1 | 2 |
| 3 | 2 |
+---------+-----+
select
from
where 行级过滤
group by 分组
having 组级过滤
order by 输出排序顺序
limit 要检索的条目数
关键字: limit
MySQL中使用limit来限制返回的条目数
一个参数,从开始处返回几条数据
select * from XXX limit 5;
mysql> select id,name,age from stus limit 3;
+----+--------------+------+
| id | name | age |
+----+--------------+------+
| 1 | 周棋洛 | 18 |
| 2 | 张郁苗 | 18 |
| 3 | 小猪佩奇 | 6 |
+----+--------------+------+
两个参数,注意检索出来的数据第一行为0而不是1,所以下面语句意思就是,从第一条数据算起,查询3条数据
select * from XXX limit 0,3;
mysql> select id,name,age from stus limit 0,2;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 周棋洛 | 18 |
| 2 | 张郁苗 | 18 |
+----+-----------+------+
mysql> select id,name,age from stus limit 2,2;
+----+--------------+------+
| id | name | age |
+----+--------------+------+
| 3 | 小猪佩奇 | 6 |
| 4 | 猪爸爸 | 8 |
+----+--------------+------+
假如规定一页有10条记录
当查询第一页时就是 limit 0,10;
当查询第二页时就是 limit 10,10;
当查询第三页时就是 limit 20,10;
所以总结一下,查询第n页数据时,limit (n-1)*每页记录数,每页记录数;
试着查询两张表
mysql> select * from stus,grade;
+----+---------+--------------+------+------------+---------+---------+----+--------------+
| id | gradeId | NAME | age | bir | english | chinese | id | grade_name |
+----+---------+--------------+------+------------+---------+---------+----+--------------+
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL | 1 | 高三一班 |
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL | 2 | 高三二班 |
| 1 | 1 | 周棋洛 | 18 | 2001-06-01 | 78 | NULL | 3 | 高一二班 |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 | 1 | 高三一班 |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 | 2 | 高三二班 |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 | 3 | 高一二班 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL | 1 | 高三一班 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL | 2 | 高三二班 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL | 3 | 高一二班 |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 | 1 | 高三一班 |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 | 2 | 高三二班 |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 | 3 | 高一二班 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 | 1 | 高三一班 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 | 2 | 高三二班 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 | 3 | 高一二班 |
+----+---------+--------------+------+------------+---------+---------+----+--------------+
15 rows in set (0.04 sec)
为啥返回给我15条结果啊❓
这样写的多表查询会有 笛卡尔积
:就是A,B两个集合,取 A,B所有的组合情况,比如A有3条记录,B有6条,查询就会有3*6=18条数据,显然不合符需求,所以要消除无效数据
关键字:INNER JOIN
连接结果仅包含符合连接条件的行组合起来作为结果集,参与连接的两个表都应该符合连接条件使用关键词:INNER JOIN 连接多张表
使用 INNER JOIN
关键字,条件使用 on
关键字
mysql> select grade_name, name from grade inner join stus on stus.gradeId = grade.id;
+--------------+--------------+
| grade_name | name |
+--------------+--------------+
| 高三一班 | 周棋洛 |
| 高三二班 | 张郁苗 |
| 高三一班 | 小猪佩奇 |
| 高一二班 | 猪爸爸 |
| 高一二班 | 猪妈妈 |
+--------------+--------------+
内连接还有一种隐式的写法,即不需要显示的指定
INNER JOIN
关键字,需要注意,使用隐式内连接条件的关键字要使用where
而不再是on
mysql> select grade_name, name from grade,stus where stus.gradeId = grade.id;
+--------------+--------------+
| grade_name | name |
+--------------+--------------+
| 高三一班 | 周棋洛 |
| 高三二班 | 张郁苗 |
| 高三一班 | 小猪佩奇 |
| 高一二班 | 猪爸爸 |
| 高一二班 | 猪妈妈 |
+--------------+--------------+
一般我们常用直接使用where关键词查询连接条件这样更方便简单
关键字:OUTER JOIN
LEFT JOIN
左(外)连接,左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录。右表记录不足的地方均为NULL
语法:
LEFT JOIN ON
LEFT OUTER JOIN ON
left join
是 left outer join
的简写
mysql> select name,grade_name from stus s left outer join grade g on s.id = g.id;
+--------------+--------------+
| name | grade_name |
+--------------+--------------+
| 周棋洛 | 高三一班 |
| 张郁苗 | 高三二班 |
| 小猪佩奇 | 高一二班 |
| 猪爸爸 | NULL |
| 猪妈妈 | NULL |
+--------------+--------------+
mysql> select name,grade_name from stus s left join grade g on s.id = g.id;
+--------------+--------------+
| name | grade_name |
+--------------+--------------+
| 周棋洛 | 高三一班 |
| 张郁苗 | 高三二班 |
| 小猪佩奇 | 高一二班 |
| 猪爸爸 | NULL |
| 猪妈妈 | NULL |
+--------------+--------------+
RIGHT JOIN
右(外)连接,右表的记录将会全部表示出来,而左表只会显示符合搜索条件的记录。左表记录不足的地方均为NULL
语法:
RIGHT JOIN ON
RIGHT OUTER JOIN ON
right join
是 right outer join
的简写
mysql> select name,grade_name from stus s right join grade g on s.id = g.id;
+--------------+--------------+
| name | grade_name |
+--------------+--------------+
| 周棋洛 | 高三一班 |
| 张郁苗 | 高三二班 |
| 小猪佩奇 | 高一二班 |
+--------------+--------------+
查询中嵌套查询,称嵌套查询为子查询
分类 (根据查询结果的不同,作用不同)
作为条件值,使用 = != < > 等进行条件判断
语法:
select 字段列表 from 表 where 字段名 = (子查询);
例如:查询stus表名字叫周棋洛的在几班
1.首先,要在stus表中查询名字为周棋洛的 gradeId 是多少
2.根据查到的 gradeId 再去 grade 查询 相对应的 grade_name
mysql> select gradeId from stus where name = "周棋洛";
+---------+
| gradeId |
+---------+
| 1 |
+---------+
mysql> select grade_name from grade where id = 1;
+--------------+
| grade_name |
+--------------+
| 高三一班 |
+--------------+
能不能把这两个sql语句合并为一个❓答案是毋庸置疑的,不可以,开玩笑的,哈哈,当然可以,要不也不说子查询了,开始合并
mysql> select grade_name from grade where id = (select gradeId from stus where name = "周棋洛");
+--------------+
| grade_name |
+--------------+
| 高三一班 |
+--------------+
作为条件值,用 in 等关键字进行条件判断
语法:
select 字段列表 from 表 where 字段名 in (子查询);
作为虚拟表
select 字段列表 from (子查询) where t;
这里是热爱动漫,热爱技术,热爱生活的小周,期待你的关注!