【第二章 DQL(数据库基础查询,条件查询,聚合函数,分组查询,排序查询,分页查询执行顺序)】

第二章 DQL(数据库基础查询,条件查询,聚合函数,分组查询,排序查询,分页查询执行顺序)

1.DQL-语法

Select 字段列表 from 表名 where 条件列表 group by 分组字段列表 havng 分组后条件列表 order by 排序字段列表 limit 分页参数

①基本查询
②条件查询(where)
③聚合函数(count,max,min,avg,sum)
④分组查询(group by)
⑤排序查询(order by)
⑥分页查询(limit)
2.基本查询

①查询多个字段
Select 字段名1,字段名2,字段名3...from 表名;
select name,workno,age from emp;
Select * from 表名;
select id,workno,name,gender,age,idcard,workaddress,entrydate from emp; 
select * from emp;
②设置别名
Select 字段名1[as 别名1],字段名2[as 别名2]...from 表名;as可以省略)
select workaddress as '工地地址' from emp;
select workaddress '工作地址' from emp;
③去除重复记录
Select distinct 字段列表 from 表名;
select distinct workaddress '工作地址' from emp;

3.条件查询
①语法: select 字段列表 from 表名 where 条件列表;
②条件:常用比较运算符
在这里插入图片描述
在这里插入图片描述
③例题:
建表

use itcast;
create table emp(
id int comment '编号',
workno  varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment'入职时间'
) comment '员工表';

插入数据

insert into emp(id,workno,name,gender,age,idcard,workaddress,entrydate)
values(1,'1','刘亚','女',20,'123456789998765111','北京','2000-01-01'),
	  (2,'2','张华','男',18,'120456787658765100','北京','2005-09-01'),
      (3,'3','魏笑','男',38,'123456789348765101','上海','2005-08-01'),
      (4,'4','赵明','女',18,'123456789568765110','北京','2009-12-01'),
      (5,'5','王茜','女',16,'124556789666876511','上海','2007-07-01'),
      (6,'6','杨浩','男',28,'121456781998765211','北京','2006-01-01'),
      (7,'7','范琪','男',40,'123456789998765201','北京','2005-05-01'),
      (8,'8','代玉','女',38,'123456789998765210','天津','2015-05-01'),
      (9,'9','刘婷','女',45,'103455789998765202','北京','2010-04-01'),
      (10,'10','陈澜','男',53,'163456783487659810','上海','2011-01-01'),
      (11,'11','张成','男',55,'612345678999876511','江苏','2015-05-01'),
      (12,'12','李博','男',32,'125456781588765701','北京','2004-02-01'),
      (13,'13','张峰','男',30,'115456781588765701','江苏','2020-11-01'),
      (14,'14','董琳','女',22,'125456781556765701','西安','2019-05-01'),
	  (15,'15','胡俊','男',32,'125433781588765701','北京','2018-04-01'),
	  (16,'16','周岚','女',39,null,'北京','2004-02-01');

表样
在这里插入图片描述

A.查询年龄等于55的员工 :
select * from emp where age=55;
B.查询年龄小于 20 的员工信息 :
select * from emp where age<20;
C.查询年龄小于等于 20 的员工信息 :
select * from emp where age<=20;
D.查询没有身份证号的员工信息 :
select * from emp where idcard is null;
E.查询有身份证号的员工信息 :
select * from emp where idcard is not null;
F.查询年龄不等于 55 的员工信息 :
select * from emp where age!=55;
select * from emp where age<>55;
G.查询年龄在15(包含)20(包含)之间的员工信息 :
select * from emp where age>=15&&age<=20;
select * from emp where age>=15 and age<=20;
select * from emp where age between 15 and 20;
H.查询性别为 女 且年龄小于 25岁的员工信息 :
select * from emp where gender='女'&& age<25;
select * from emp where gender='女' and age<25;
I.查询年龄等于182040 的员工信息 :
select * from emp where age =18||age=20||age=40;
select * from emp where age =18 or age=20 or age=40;
J.查询姓名为两个字的员工信息 _ % :(一个'_'代表一个字符)
select * from emp where name like '_ _';
K.查询身份证号最后一位是1的员工信息 :(%1表示1之前不重要,只要末尾是1即可)
select * from emp where idcard like '%1';

4.聚合函数
①将一列数据作为一个整体,进行纵向计算。
②常见聚合函数:
在这里插入图片描述③语法:

SELECT 聚合函数(字段列表) FROM 表名 ;
注意 : NULL值是不参与所有聚合函数运算的。

④例题:

表样如上:
A. 统计该企业员工数量
select count(*) from emp;#整张表的数据
select count(id) from emp;#以id为字段进行统计
B. 统计该企业员工的平均年龄
select avg(age) from emp;
C. 统计该企业员工的最大年龄
select max(age) from emp;
D. 统计该企业员工的最小年龄
select min(age) from emp;
E. 统计西安地区员工的年龄之和
select sum(age) from emp where workaddress='西安';

5.分组查询
①语法:

SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组后过滤条件 ];

②where与having区别
a.执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。
b.判断条件不同:where不能对聚合函数进行判断,而having可以。
③注意事项:
• 分组之后,查询的字段一般为聚合函数分组字段,查询其他字段无任何意义。
• 执行顺序: where > 聚合函数 > having
• 支持多字段分组, 具体语法为 : group by columnA,columnB
④例题:

A. 根据性别分组 , 统计男性员工和女性员工的数量
select  gender,count(*) from emp group by gender;
B. 根据性别分组 , 统计男性员工和女性员工的平均年龄
select  gender,avg(age) from emp group by gender;
C. 查询年龄小于35的员工 , 并根据工作地址分组 , 获取员工数量大于等于3的工作地址
select  workaddress,count(*) from emp where age<35 group by workaddress having count(*)>=3;

6.排序查询:
①语法:

SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1 , 字段2 排序方式2 ;

②排序方式:
ASC : 升序(默认值)
DESC: 降序

注意事项:
• 如果是升序, 可以不指定排序方式ASC ;
• 如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序 ;
③例题:

A. 根据年龄对公司的员工进行升序排序
select * from emp order by age asc;
B. 根据入职时间, 对员工进行降序排序
select * from emp order by entrydate desc;
C. 根据年龄对公司的员工进行升序排序 , 年龄相同 , 再按照入职时间进行降序排序
select * from emp order by age asc,entrydate desc;

7.分页查询:
①语法:

SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数 ;

②注意事项:
• 起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数。
• 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT。
• 如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10。
③例题:

A. 查询第1页员工数据, 每页展示10条记录
select * from emp limit 0,10;
B. 查询第2页员工数据, 每页展示10条记录 --------> (页码-1)*页展示记录数
select * from emp limit 10,10;

8.案例练习:

(1). 查询性别为女,并且年龄为20,21,22,23岁的员工信息。
select * from emp where gender='女'  and age in(21,21,22,23);
(2). 查询性别为 男 ,并且年龄在 20-40()以内的姓名为三个字的员工。
select * from emp where gender='男' and (age between 20 and 40) and name like'_ _ _';
(3). 统计员工表中, 年龄小于50岁的 , 男性员工和女性员工的人数。
select gender,count(*) from emp where age<50 group by gender;
(4). 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序。
select name,age  from emp where age<=35 order by age asc,entrydate desc;
(5). 查询性别为男,且年龄在20-40()以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序。
select * from emp where gender ='男'and (age between 20 and 40) order by age asc,entrydate asc limit 5;

9.DQL语句执行顺序
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值