第三章 DQL数据查询语言

重点,DQL是我们每天接触编写最多也是最难的sql,该语言用来查询记录,不会修改数据库和表结构:

一、构建数据库

学习之前我们需要创建数据库并填充部分数据:

drop TABLE if EXISTS student;
CREATE TABLE student (id INT(10) PRIMARY key,name VARCHAR (10),age INT (10) NOT NULL,gander varchar(2)
);drop TABLE if EXISTS course;
CREATE TABLE course (id INT (10)  PRIMARY key,name VARCHAR (10) ,t_id INT (10)
) ;drop TABLE if EXISTS teacher;
CREATE TABLE teacher(id INT (10)  PRIMARY key,name VARCHAR (10)
);drop TABLE if EXISTS scores;
CREATE TABLE scores(s_id INT ,score INT (10),c_id INT (10) ,PRIMARY key(s_id,c_id)
) ;

表单填充数据:

insert into  student (id,name,age,gander)VALUES(1,'白杰',19,'男'),(2,'连宇栋',19,'男'),(3,'邸志伟',24,'男'),(4,'李兴',11,'男'),(5,'张琪',18,'男'),(6,'武三水',18,'女'),(7,'张志伟',16,'男'),(8,'康永亮',23,'男'),(9,'杨涛瑞',22,'女'),(10,'王杰',21,'男');insert into  course (id,name,t_id)VALUES(1,'数学',1),(2,'语文',2),(3,'c++',3),(4,'java',4),(5,'php',null);insert into  teacher (id,name)VALUES(1,'张楠'),(2,'李子豪'),(3,'薇薇姐'),(4,'猴哥'),(5,'八戒');insert into  scores (s_id,score,c_id)VALUES(1,80,1);
insert into  scores (s_id,score,c_id)VALUES(1,56,2);
insert into  scores (s_id,score,c_id)VALUES(1,95,3);
insert into  scores (s_id,score,c_id)VALUES(1,30,4);
insert into  scores (s_id,score,c_id)VALUES(1,76,5);insert into  scores (s_id,score,c_id)VALUES(2,35,1);
insert into  scores (s_id,score,c_id)VALUES(2,86,2);
insert into  scores (s_id,score,c_id)VALUES(2,45,3);
insert into  scores (s_id,score,c_id)VALUES(2,94,4);
insert into  scores (s_id,score,c_id)VALUES(2,79,5);insert into  scores (s_id,score,c_id)VALUES(3,65,2);
insert into  scores (s_id,score,c_id)VALUES(3,85,3);
insert into  scores (s_id,score,c_id)VALUES(3,37,4);
insert into  scores (s_id,score,c_id)VALUES(3,79,5);insert into  scores (s_id,score,c_id)VALUES(4,66,1);
insert into  scores (s_id,score,c_id)VALUES(4,39,2);
insert into  scores (s_id,score,c_id)VALUES(4,85,3);insert into  scores (s_id,score,c_id)VALUES(5,66,2);
insert into  scores (s_id,score,c_id)VALUES(5,89,3);
insert into  scores (s_id,score,c_id)VALUES(5,74,4);insert into  scores (s_id,score,c_id)VALUES(6,80,1);
insert into  scores (s_id,score,c_id)VALUES(6,56,2);
insert into  scores (s_id,score,c_id)VALUES(6,95,3);
insert into  scores (s_id,score,c_id)VALUES(6,30,4);
insert into  scores (s_id,score,c_id)VALUES(6,76,5);insert into  scores (s_id,score,c_id)VALUES(7,35,1);
insert into  scores (s_id,score,c_id)VALUES(7,86,2);
insert into  scores (s_id,score,c_id)VALUES(7,45,3);
insert into  scores (s_id,score,c_id)VALUES(7,94,4);
insert into  scores (s_id,score,c_id)VALUES(7,79,5);insert into  scores (s_id,score,c_id)VALUES(8,65,2);
insert into  scores (s_id,score,c_id)VALUES(8,85,3);
insert into  scores (s_id,score,c_id)VALUES(8,37,4);
insert into  scores (s_id,score,c_id)VALUES(8,79,5);insert into  scores (s_id,score,c_id)VALUES(9,66,1);
insert into  scores (s_id,score,c_id)VALUES(9,39,2);
insert into  scores (s_id,score,c_id)VALUES(9,85,3);
insert into  scores (s_id,score,c_id)VALUES(9,79,5);insert into  scores (s_id,score,c_id)VALUES(10,66,2);
insert into  scores (s_id,score,c_id)VALUES(10,89,3);
insert into  scores (s_id,score,c_id)VALUES(10,74,4);
insert into  scores (s_id,score,c_id)VALUES(10,79,5);

二、单表查询

1、基本查询
(1)基本语法

查询所有列,其中*表示查询所有列,而不是所有行的意思:

select * from 表名;

查询指定列:

select 列1,列2,列n from 表名;
select `id`,`name`,`age`,`gander` from `student`;
select `id`,`name`,`age` from `student`;

完全重复的记录只显示一次,在查询的列之前添加distinct

(2)列运算

数量类型的列可以做加、减、乘、除等运算:

-- 查询给所有员工工资加1000的结果
select id,name,sal+1000 from employee;
select `id`,`name`,`age`*10 from student;

说明:

1、null加任何值都等于null,,需要用到ifnull()函数。SELECT IFNULL(sal,0) from 表名; 如果薪资列为空,则输出0;

2、将字符串做加减乘除运算,会把字符串当作0。

(3)别名

我们可以给列名起【别名】,因为在查询的结果中列名可能重复,可能名字不够简洁,或者列的名字不满足我们的要求:

select 列名1 (as) 别名1,列名2 (as) 别名2 from 表名;
select `id` `编号`,`name` `名字`,ifnull(`age`,0) as `age` from `student` as s;

只需要在列名后加 as 新列名 ,或是直接加上 新列名即可。

(4)条件控制

条件查询:在后面添加where指定条件,我们在学习update语句时,接触过这里大致是一样的:

-- 条件控制  select * from 表名 where 列名=指定值;
select * from student where id = 3;
select * from student where id in (1,3,7);
select * from student where id >5 ;
select * from student where id between 3 and 7 ;
select * from student where id between 6 and 7 or age > 20;

模糊查询:当你想查询所有姓张的记录。用到关键字like。

select * from student where name like '张_';
select * from student where name like '张%';

(_代表匹配任意一个字符,%代表匹配0~n个任意字符)
2、排序(所谓升序和降序都是从上往下排列)

  • 升序: ascend
select * form 表名 order by 列名 asc;     asc为默认值可以不写

-降序:descend

select * from 表名 order by 列名 desc;
  • 使用多列作为排序条件: 当第一列排序条件相同时,根据第二列排序条件排序(当第二列依旧相同时可视情况根据第三例条件排序):
select * from 表名 order by 列名1 asc, 列名2 desc;

意思是当列名1的值相同时按照列名2的值降序排。

3、聚合函数
1.count:查询满足条件的记录行数,后边可以跟where条件:

如果使用的列值为空,不会进行统计,

我们如果统计真实的表记录条数,最好不要用可以为空的列:

count(*) count(id) count(1)

select count(列名) from 表名;
select max(age) from student where id > 5;

2.max:查询满足条件的记录中的最大值,后边可以跟where条件:

select max(列名) from 表名;

3.min:查询满足条件的记录中的最大值,后边可以跟where条件:

select min(列名) from 表名;

4.sum:查询满足条件的记录中的值的和,后边可以跟where条件:

select sum(列名) from 表名;

5.avg:查询满足条件的记录中的值的平均数,后边可以跟where条件:

select avg(列名) from 表名;

#4、分组查询
顾名思义:分组查询就是将原有数据进行分组统计:

我们举一个例子:将班级的同学按照性别分组,统计男生和女生的平均年龄和成绩。这就是一个典型的分组查询。

基本语法:

select 分组列名,聚合函数1,聚合函数2 from 表名 group by 该分组列名;

分组要使用关键词group by,后边可以是一列,也可以是多个列,分组后查询的列只能是分组的列,或是使用了聚合函数的其他列,其他列不能单独使用。

我们可以这样理解:一旦发生了分组,我们查询的结果只能是所有男生的成绩总和、平均值,而不能查询某一个男生的成绩。

有时我们需要对数据进行帅选,作为分组条件的列和聚合函数:

分组查询前,还可以通过关键字【where】先把满足条件的人分出来,再分组,语法为:

select 分组列,聚合函数 from 表名 where 条件 group by 分组列;

分组查询后,也可以通过关键字【having】把组信息中满足条件的组再细分出来,语法为:

select 分组列,聚合函数 from 表名 where 条件 group by 分组列 having 聚合函数或列名(条件);

例子:

select gander,avg(age) avg_age,sum(age) sum_age from student GROUP BY gander HAVING  gander = '男'

5、LIMIT子句
LIMIT用来限定查询结果的起始行,以及总行数,通常用来做分页查询,他是mysql中独有的语法。

例如:

select * from 表名 limit 4,3;

如果一个参数:说明从开始查找三条记录

SELECT id,name,age,gander FROM student limit 3;

如果两个参数:说明从第三行起(不算),向后查三条记录

SELECT id,name,age,gander FROM student limit 3,3;

Mysql入门学习第三章(学习DQL语句)相关推荐

  1. mysql入门很简单系列视频-学习笔记

    mysql入门很简单系列视频-学习笔记 视频链接:mysql入门很简单系列视频 https://www.bilibili.com/video/av14920200/ 以前主要就了解DDL.DML.DC ...

  2. MySQL学习记录 (三) ----- SQL数据定义语句(DDL)

    相关文章: <MySQL学习记录 (一) ----- 有关数据库的基本概念和MySQL常用命令> <MySQL学习记录 (二) ----- SQL数据查询语句(DQL)> &l ...

  3. 《数字图像处理》第三章学习总结感悟2:直方图处理

    ☞ ░ 前往老猿Python博文目录 https://blog.csdn.net/LaoYuanPython ░ 一.引言 在2021年6月22日发布<<数字图像处理>第三章学习总结 ...

  4. 深度学习 第三章 tensorflow手写数字识别

    深度学习入门视频-唐宇迪 (笔记加自我整理) 深度学习 第三章 tensorflow手写数字识别 1.tensorflow常见操作 这里使用的是tensorflow1.x版本,tensorflow基本 ...

  5. Java学习 第三章 数组(三)排序算法

    ** Java学习 第三章 数组(三)排序算法 ** 主要内容:排序算法.排序算法横向比较.Arrays工具类的使用.数组常见异常 1.数组中涉及到的常见算法:排序算法 1.1 排序算法分类:内部排序 ...

  6. 工程伦理第三章学习笔记2020最新

    工程伦理第三章学习笔记2020最新 因为之前自己在网上找答案总是觉得费劲,一道一道的找,很慢,突然找到了前两章的答案,感觉有一种前人种树后人乘凉的感觉,于是自己在艰难找完第三章习题并全对的情况下,将题 ...

  7. 多维随机变量及其分布——《概率论及其数理统计》第三章学习笔记

    多维随机变量及其分布--<概率论及其数理统计>第三章学习笔记 文章目录 多维随机变量及其分布--<概率论及其数理统计>第三章学习笔记 前言 MindMap 二维随机变量 定义与 ...

  8. 周志华西瓜书第三章学习笔记

    第三章学习笔记 文章目录 第三章学习笔记 1.知识脉络 2.我的笔记 参考 1.知识脉络 2.我的笔记 这一章公式推导实在太多了,需要补充的推导过程也有很多,就不写电子档了.扩展公式推导和LDA部分补 ...

  9. Python爬虫学习第三章-4.3-使用xpath解析爬取全国城市名称

    Python爬虫学习第三章-4.3-使用xpath解析爬取全国城市名称   这一节主要是使用xpath解析爬取全国城市名称 这里使用的网址是:空气质量历史数据查询   这一个案例体现的点主要是xpat ...

最新文章

  1. 三亚之旅 海景--美得让你尖叫
  2. Go gin获取GET请求参数
  3. nearbyserversocket驱动_关于Socket和ServerSocket类详解
  4. Unity协程(Coroutine)原理深入剖析再续
  5. php中nodethirtythree,node常用模块 - LinearLaw的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. 存根类 测试代码 java_嘲弄和存根–了解Mockito的测试双打
  7. java hibernate 插入数据_[Java教程]hibernate 返回新插入数据的Id
  8. java方法的重载 编程题,java面试编程题:重载方法
  9. Linux和win7双系统时间错误问题
  10. JS 获得FileUpload1 的完整路径
  11. nginx中$host、$http_host和$proxy_host区别
  12. 用C++做数据分析 - 唐代诗人的朋友圈
  13. Xenu简单使用说明
  14. java 最短遍历路径_凯文培根游戏的最短路径图遍历
  15. 家族关系查询系统程序设计算法思路_数据结构课程设计(家族关系查询系统).....
  16. 导数的四则运算和复合函数的求导
  17. eNSP下园区网综合实验分步配置(前言)
  18. 【C++】特殊类设计
  19. 区块链杂谈---默克尔树(Merkle Tree)解析
  20. 计算机表格数据处理,《计算机操作基础》第四章 Excel表格数据处理课后练习.docx...

热门文章

  1. QCon北京2018,PPT资源下载。人工智能/大数据/软件结构/高并发架构/区块链/k8s等相关技术以及解决方案应有尽有
  2. vivo y系列发展史(第一章——too late老手机)
  3. 怎样用计算机产生均匀随机数,掌握利用计算器计算机产生均匀随机数的方法.PPT...
  4. Altera 的GPP LTE 基站解决方案
  5. python异步任务
  6. MybatisPuls学习笔记
  7. php中跳转语句各自的特点,下列选项中,列举PHP中跳转语句错误的是( )。
  8. SQLite数据库介绍与使用
  9. 小数在内存中究竟是如何存储的(C语言代码详细讲解 2)
  10. 二、大端模式和小端模式