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;
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 1. 了解SQL 1.1 数据库基础 ​ 学习到目前这个阶段,我们就需要以某种方式与数据库打交道。在深入学习MyS...
    锋享前端阅读 991评论 0 1
  • 手动不易,转发请注明出处 --Trance 数据库系统命令: (1).查看存储过程状态:show pro...
    Trance_b54c阅读 1,592评论 0 8
  • 一、上堂回顾 1.概念​ 数据库管理系统,数据库,表​ SQL的分类:DDL、DML、DQL、DCL2.数据库的使...
    WenErone阅读 384评论 0 0
  • 正在琢磨今天的日更写点儿什么?一个关注的简友,突然发了一篇文章《不欺骗自己,停止日更》 原因主要还是为了保证文字的...
    老可是个传说阅读 278评论 1 6
  • 秦川抬头看见二狗,面露笑容,尽管前一秒还是疲倦,他往前跑了两步,来到大门前,同时看到了章疯子,先喊了一声章爷爷,然...
    金大铮阅读 98评论 0 0