MySQL--DQL语句使用--Day4

MySQL--DQL语句使用--Day4

上节重点回顾:

1、数据类型
2、列属性
3、各种SQL语句使用场景
4、命令笔记记不住


一、DQL语句使用

1、select语句

1.1、作用
获取MySQL中的数据行

1.2、 单独使用select
1.2.1 select @@xxxx;
mysql > select @@port;                                      #查mysql数据的端口
mysql > show  variables like '%innodb%';           
1.2.2 select 函数();
mysql > select now();                                          #查询时间   
mysql > select version();                                     #查询版本信息

1.3 SQL92标准的使用语法
1.3.1 select 语法执行顺序(单表)
select开始  ------> 
from字句 ------>
where字句 ------>
group by字句 ------> 
select 后执行条件------> 
having 字句 ------>
order by ------> 
limit


二、select语句应用

from字句

1.3.2、from
-- 例子:查询city表中的所有数据
use school;
select  * from score; --->适合表数据较少,生产中使用较小。
select  * from school.score;  --->使用绝对路径

-- 例子:查询sno和score的所有值
select  sno , score from score;
select  sno , score from school.score; --->使用绝对路径

--- 单表查询练习环境:world数据库下表介绍
show databases ;
show tables  from  world;
use world;
select * from world.city;

city(城市)
desc city;
id:自增的无关列,数据行的需要
NAME:城市名字
countrycode:城市所在的国家代号,CHN,USA,JPN;
district:中国省的意思,美国是洲
population:城市的人口数量

country(国家)
countrylanguage(国家语言)

入职DBA技巧

熟悉业务:
刚入职时,DBA的任务
1、通过公司架构图,搞清楚数据库的物理结构
1-2天
逻辑结构:
(1)生产库的信息(容易达到)
(2)库下表的信息(非常复杂)
1、开发和业务人员,搞好关系
2、搞到ER图(PD)
3、啥都没有怎么办?
(1)找到建表语句,如果有注释。如果没有注释,只能根据列名翻译
(2)找到表中部分数据,分析数据特点,达到了解列功能的目录


where字句

1.3.3、where字句
-- 例子
-- where 配合 等值查询
-- 查询 city 表中,中国的城市信息
select * from  world.city where CountryCode='chn';
-- 查询 city 表中,查询美国的城市信息
select * from  world.city where CountryCode='usa';

-- where 配合 不等于
-- 查询一下世界人口小于100人的城市
select * from  world.city where population<100;
-- 查询世界人口大于10000000的城市
select * from  world.city where population>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 world.city where  Population > 10000 AND  Population < 20000;
select  * from world.city where  Population between 10000 and 20000;

-- 查询中国或美国的城市信息
select * from  world.city where  CountryCode='chn' or CountryCode='usa';
select * from  world.city where  CountryCode in ('cha','usa');
建议改写成,一下语句
select *
from  world.city
where CountryCode='chn';
UNION ALL
select *
from  world.city
where CountryCode='usa';


group by字句

1.3.4 group by 配合聚合函数引应用
常用聚合函数:
AVG()  平均
COUNT() 计数
SUM()   总数
MAX()   最大
MIN()   最小
GROUP_CONCAT() 列转行

-- 统计每个国家的总人口
select  countrycode,sum(Population) from  world.city group by CountryCode;

-- 统计每个国家的城市个数
1、拿什么站队
group by CountryCode
2、拿什么统计
城市id,name
3、统计的是什么?
count(id)
select  CountryCode,count(Name)from  world.city group by CountryCode;

-- 统计并显示,每个国家的省的省名字列表
select  CountryCode,group_concat(District)from  world.city group by CountryCode;

-- 统计中国省的城市列表
select  District,group_concat(name)from world.city where CountryCode='CHN'group by District;

-- 统计一下中国每个省的总人口数
select  District,group_concat(population)
from world.city
where CountryCode='CHN'
group by District;

-- 统计一下中国,每个省的国家的平均数
select  District,avg(population)
from world.city
where CountryCode='CHN'
group by District;


HAVING字句

1.3.5 HAVING

-- 统计中国,每个省的总人口大于1000w的省及人口数
select  District,group_concat(population)
from world.city
where CountryCode='CHN'
group by District
having sum(Population)>10000000;
说明: having后的条件是不走索引的,可以进行一些优化手段处理。


ORDER BY字句

1.3.6 ORDER BY
select  District,sum(population)
from world.city
where CountryCode='CHN'
group by District
ORDER BY sum(Population) desc ;

说明:desc 从大到小

--- 例子:查询中国所有的城市,并以人口数降序输出
select *
from world.city
where countryCode='chn'
order by  Population desc;


LIMIT字句

1.3.7 LIMIT

--- 例子:查询中国所有的城市,并以人口数降序输出,并显示前10行
select *
from world.city
where countryCode='chn'
order by  Population desc
limit 10;

select *
from world.city
where countryCode='chn'
order by  Population desc
limit 5,5;

select *
from world.city
where countryCode='chn'
order by  Population desc
limit 5 offset 5;


1.4 多表连接查询

语法

image

1.4.1 介绍4张测试表的关系

image

1.4.2 什么时候用?
需要查询的数据来自于多张表时。


1.4.3 怎么多表连接查询

传统的连接:基于where条件
1、找表之间的关系列
2、排列查询条件
selcet name,countrycode from city whree population<100;
pcn
selcet name,surfacearea from country whree code<'pcn';

-- 人口数量小于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人国家名,城市名,国土面积
use world;
select country.name,city.name,country.surfacearea
from
city join country
on city.countrycode=country.code
where city.population<100;

-- 2、查询oldguo老师和他教的课程名称
use school;
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';

show  tables;
select * from course;  课程表
select * from score;   成绩表
select * from student; 学生表
select * from teacher; 教师表

--- 3\. 统计一下每门课程的总成绩
select course.cname,sum(score.score)
from course
join score
on course.cno = score.cno
group by course.cno,course.cname;

5.7、版本会报错情况
mysql> select course.cno,course.cname,sum(score.score)
    -> from course
    -> join score
    -> on score.cno=course.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(score.score)
from course
join score
on score.cno=course.cno
group by course.cno;

select @@sql_mode;       #查看sql_mode的

--- 4\. 查询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;

--- 5\. 查询所有老师教的学生姓名列表
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;

--- 6\. 查询oldboy老师教的不及格学生的姓名
SELECT teacher.tname,GROUP_CONCAT(student.sname)
FROM student
JOIN score
ON student.sno=score.sno
JOIN course
ON score.cno=course.cno
JOIN teacher ON
course.tno=teacher.tno
WHERE teacher.tname='oldboy' AND score.score<60
GROUP BY teacher.tno;

SELECT
    course.cname,
    teacher.tname,
    student.sname
FROM
    teacher
    JOIN course ON teacher.tno = course.tno
    JOIN score ON course.cno = score.cno
    JOIN student ON score.sno = student.sno
WHERE
    teacher.tname = 'oldboy'
AND
    score.score < 60;

--- 7\. 统计zhang3,学习了几门课
SELECT student.sname,GROUP_CONCAT(course.cname)
FROM teacher
  JOIN course
    ON teacher.tno = course.tno
  JOIN score
    ON course.cno = score.cno
  JOIN student
    ON score.sno = student.sno
WHERE student.sname = 'zhang3'
GROUP BY student.sname;

--- 8\. 查询zhang3,学习的课程名称有哪些?
select student.sname,group_concat(course.cname)
from student
  join score
    on student.sno=score.sno
  join course
    on score.cno=course.cno
where student.sname='zhang3'
group by student.sno;

--- 9\. 查询oldguo老师教的学生名.
select teacher.tname,group_concat(student.sname)
from teacher
  join course
    on teacher.tno=course.tno
  join score
    on course.cno=score.cno
    join student
      on score.sno=student.sno
where teacher.tname='oldguo'
group by teacher.tno;

--- 10.查询oldguo所教课程的平均分数
select teacher.tname,AVG(score.score)
from course
  join teacher
    on course.tno=teacher.tno
  join score
    on course.cno=score.cno
where teacher.tname='oldguo'
group by teacher.tno or  course.cno;

--- 11.每位老师所教课程的平均分,并按平均分排序
select teacher.tname,course.cname,AVG(score.score)
from course
  join teacher
    on course.tno=teacher.tno
  join score
    on course.cno=score.cno
group by course.cno
ORDER BY AVG(score.score);

--- 12.查询oldguo所教的不及格的学生姓名
select teacher.tname,student.sname,course.cname,score.score
from course
  join teacher
    on course.tno=teacher.tno
  join score
    on course.cno=score.cno
    join student
      on score.sno=student.sno
where teacher.tname='oldguo' and score.score<60;
group by teacher.tno

--- 13.查询所有老师所教学生不及格的信息
select teacher.tname,student.sname
from student
  join score
    on student.sno=score.sno
  join course
    on course.cno=score.cno
  JOIN teacher
    on teacher.tno=course.tno
WHERE score.score<60
group by teacher.tname,student.sname;

你可能感兴趣的:(MySQL--DQL语句使用--Day4)