上节重点难点回顾:
1. 数据类型
2. 列属性
primary key
unique
not null
default
auto_increment
unsigned
comment
3. 各种SQL语句使用场景
DDL: create database create table drop database drop table alter database alter table truncate
DML : insert update delete
insert into oldguo(name,age,gender)
values
('zhangsan',18,'m'),
('zhs',18,'m');
update where
delete where
4. 命令记不住
命令太长
collation 校对规则
大小写是否敏感
=============================我是分割线===============================
1. select
1.1 作用
获取MySQL中的数据行
1.2 单独使用select
1.2.1 select @@xxxx;获取参数信息。
mysql> select @@port;
mysql> show variables like '%innodb%';
1.2.2 select 函数();
mysql> select database();
mysql> select now();
mysql> select version();
1.3 SQL92标准的使用语法
1.3.1 select语法执行顺序(单表)
select开始 ---->
from子句 --->
where子句--->
group by子句--->
select后执行条件--->
having子句 ---->
order by ---->
limit
1.3.2 FROM
--- 例子:查询city表中的所有数据
USE world;
SELECT * FROM city; --->适合表数据行较少,生产中使用较少。
SELECT * FROM world.city;
--- 例子: 查询name和population的所有值
SELECT NAME , population FROM city;
SELECT NAME , population FROM world.city;
单表查询练习环境:world数据库下表介绍
SHOW TABLES FROM world;
city(城市):
DESC city;
id: 自增的无关列,数据行的需要
NAME: 城市名字
countrycode:城市所在的国家代号,CHN,USA,JPN。。。。
district : 城市的所在的区域,中国是省的意思,美国是洲的意思
population: 城市的人口数量
说明: 此表是历史数据,仅供学习交流使用。
熟悉业务:
刚入职时,DBA的任务
1. 搞清楚架构
通过公司架构图,搞清楚数据库的物理架构
1-2天
2.逻辑结构:
(1)生产库的信息(容易达到)
(2)库下表的信息(非常复杂)
1. 开发和业务人员,搞好关系
2. 搞到ER图(PD)
3. 啥都没有怎么怎么办?
(1) 找到建表语句,如果有注释,读懂注释。如果没有注释,只能根据列名翻译
(2) 找到表中部分数据 ,分析数据特点,达到了解列功能的目录
1.3.3 where
--- 例子:
--- WHERE 配合 等值查询(=)
--- 查询中国的城市信息
SELECT *
FROM world.city
WHERE countrycode='CHN';
--- 查询美国的城市信息
SELECT *
FROM world.city
WHERE countrycode='USA';
--- WHERE 配合 不等值(> < >= <= <>)
--- 查询一下世界上人口小于100人的城市
SELECT *
FROM world.city
WHERE population<100;
--- 查询世界上人口大于10000000的城市
略。
--- WHERE 配合 模糊(LIKE)
--- 查询国家代号是C开头的城市
SELECT *
FROM world.city
WHERE countrycode
LIKE 'C%';
--- 注意:like 语句在MySQL中,不要出现%在前面的情况。因为效率很低,不走索引。
--- 错误的里
SELECT *
FROM world.city
WHERE countrycode
LIKE '%C%';
--- WHERE 配合 逻辑连接符(AND OR)
--- 查询城市人口在1w到2w之间的城市
SELECT *
FROM city
WHERE population >= 10000
AND Population <= 20000;
SELECT *
FROM city
WHERE population
BETWEEN 10000 AND 20000;
--- 查询一下中国或美国的城市信息
SELECT *
FROM city
WHERE countrycode='CHN' OR countrycode='USA';
SELECT *
FROM city
WHERE countrycode IN ('CHN','USA');
UNION ALL :不会去重,显示全部;UNION:会去重,只显示不重复的。
建议改写为,以下语句,查询性能好:
SELECT *
FROM city
WHERE countrycode='CHN'
UNION ALL
SELECT *
FROM city
WHERE countrycode='USA';
1.3.4 GROUP BY 配合聚合函数应用
常用聚合函数:
AVG()
COUNT()
SUM()
MAX()
MIN()
GROUP_CONCAT()
--- 统计每个国家的总人口
SELECT countrycode,SUM(population) FROM city GROUP BY countrycode ;
--- 统计每个国家的城市个数
1.拿什么站队
GROUP BY countrycode
2. 拿什么统计
城市id,name
3. 统计的是什么?
COUNT(id)
SELECT countrycode,COUNT(id) FROM city GROUP BY countrycode;
--- 统计并显示 每个国家的省名字列表
SELECT countrycode,GROUP_CONCAT(district) FROM city GROUP BY countrycode;
--- 统计中国每个省的城市名列表
SELECT District,GROUP_CONCAT(NAME)
FROM city
WHERE countrycode='CHN'
GROUP BY district;
--- 统计一下中国,每个省的总人口数
SELECT district ,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
--- 统计一下中国,每个省的平均人口
SELECT district ,AVG(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
1.3.5 HAVING
--- 统计中国,每个省的总人口大于1000w的省及人口数
SELECT district ,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
HAVING SUM(population)>10000000
说明: having后的条件是不走索引的,可以进行一些优化手段处理。
1.3.6 ORDER BY
SELECT district ,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
ORDER BY SUM(population) DESC ;
--- 例子:查询中国所有的城市,并以人口数降序输出
SELECT * FROM city WHERE countrycode='CHN' ORDER BY population DESC;
1.3.7 LIMIT
SELECT *
FROM city
WHERE countrycode='CHN'
ORDER BY population DESC
LIMIT 5;
SELECT *
FROM city
WHERE countrycode='CHN'
ORDER BY population DESC
LIMIT 10;
SELECT *
FROM city
WHERE countrycode='CHN'
ORDER BY population DESC
LIMIT 5,3;
SELECT *
FROM city
WHERE countrycode='CHN'
ORDER BY population DESC
LIMIT 3 OFFSET 5;
LIMIT M,N 跳过M行,显示N行
LIMIT X OFFSET Y 跳过Y行,显示X行
1.4 多表连接查询
1.4.1 介绍4张测试表的关系
1.4.2 什么时候用?
需要查询的数据是来自于多张表时。
1.4.3 怎么去多表连接查询
(1)传统的连接:基于where条件
1. 找表之间的关系列
2. 排列查询条件
select name,countrycode from city whrere population<100;
PCN
select name,surfacearea from country where code='PCN'
--- 人口数量小于100人的城市,所在国家的国土面积(城市名,国家名,国土面积)
select city.name,country.name ,country.surfacearea
from city,country
where city.countrycode = country.code
and city.population<100
(2)内连接 *****
A B
A.x B.y
1. 找表之间的关系列
2. 将两表放在join左右
3. 将关联条件了放在on后面
4. 将所有的查询条件进行罗列
select A.m,B.n
from
A join B
on A.x=B.y
where
group by
order by
limit
--- 例子:
--- 1. 查询人口数量小于100人的国家名,城市名,国土面积
SELECT country.name,city.name,country.surfacearea
FROM
city JOIN country
ON city.countrycode=country.code
WHERE city.population<100;
- 查询oldguo老师和他教课程名称
SELECT teacher.tname ,course.cname
FROM teacher
JOIN course
ON teacher.tno=course.tno
WHERE teacher.tname='oldguo';
SELECT teacher.`tname` ,course.`cname`
FROM teacher
JOIN course
ON teacher.`tno`=course.`tno`
WHERE teacher.`tname`='oldboy';
- 统计一下每门课程的总成绩
SELECT course.cname,SUM(sc.score)
FROM course
JOIN sc
ON course.cno = sc.cno
GROUP BY course.cname;
-- 5.7 版本会报错的情况,在sqlyog中以下操作没问题
-- 但是在命令行上是会报错
SELECT course.cno,course.cname,SUM(sc.score)
FROM course
JOIN sc
ON course.cno = sc.cno
GROUP BY course.cname;
mysql> SELECT course.cno,course.cname,SUM(sc.score)
-> FROM course
-> JOIN sc
-> ON course.cno = sc.cno
-> GROUP BY course.cname;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'school.course.cno' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
1. 在select后面出现的列,不是分组条件,并且没有在函数中出现。
2. 如果group by 后是主键列或者是唯一条件列,不会报出错误。如下:
SELECT
course.cno,course.cname,SUM(sc.score) FROM course
JOIN sc
ON course.cno = sc.cno
GROUP BY course.cno;
(3)外链接 ****
自连接(自己了解)
- 查询oldguo老师教的学生姓名列表
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM teacher
JOIN course
ON teacher.tno = course.tno
JOIN sc
ON course.cno = sc.cno
JOIN student
ON sc.sno = student.sno
WHERE teacher.tname='oldguo'
GROUP BY teacher.tname;
- 查询所有老师教的学生姓名列表
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM teacher
JOIN course
ON teacher.tno = course.tno
JOIN sc
ON course.cno = sc.cno
JOIN student
ON sc.sno = student.sno
GROUP BY teacher.tno;
- 查询oldboy老师教的不及格学生的姓名
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM teacher
JOIN course
ON teacher.tno = course.tno
JOIN sc
ON course.cno = sc.cno
JOIN student
ON sc.sno = student.sno
WHERE teacher.tname='oldboy' AND sc.score<60
GROUP BY teacher.tno;
- 统计zhang3,学习了几门课
SELECT student.`sname` ,COUNT(sc.`cno`)
FROM student
JOIN sc
ON student.`sno`=sc.`sno`
WHERE student.sname='zhang3';
- 查询zhang3,学习的课程名称有哪些?
SELECT student.sname,GROUP_CONCAT(course.`cname`)
FROM student
JOIN sc
ON student.`sno`=sc.`sno`
JOIN course
ON sc.`cno`=course.`cno`
WHERE student.`sname`='zhang3';
- 查询oldguo老师教的学生名.
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM teacher
JOIN course
ON teacher.tno = course.tno
JOIN sc
ON course.cno = sc.cno
JOIN student
ON sc.sno = student.sno
WHERE teacher.tname='oldguo'
GROUP BY teacher.tname;
10.查询oldguo所教课程的平均分数
SELECT teacher.tname ,course.`cname`,AVG(sc.`score`)
FROM teacher
JOIN course
ON teacher.`tno`=course.`tno`
JOIN sc
ON course.`cno`=sc.`cno`
WHERE teacher.tname='oldguo';
11.每位老师所教课程的平均分,并按平均分排序
SELECT teacher.tname ,course.`cname`,AVG(sc.`score`)
FROM teacher
JOIN course
ON teacher.`tno`=course.`tno`
JOIN sc
ON course.`cno`=sc.`cno`
ORDER BY AVG(sc.`score`);
12.查询oldguo所教的不及格的学生姓名
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM teacher
JOIN course
ON teacher.tno = course.tno
JOIN sc
ON course.cno = sc.cno
JOIN student
ON sc.sno = student.sno
WHERE teacher.tname='oldguo' AND sc.score<60
GROUP BY teacher.tno;
13.查询所有老师所教学生不及格的信息
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM teacher
JOIN course
ON teacher.tno = course.tno
JOIN sc
ON course.cno = sc.cno
JOIN student
ON sc.sno = student.sno
WHERE sc.score<60;