玖叶教程网

前端编程开发入门

[MySQL] SQL语句-DQL语句详解

#头条创作挑战赛#

DQL语句

创建新表

create table testtable(id int ,name varchar(20));
alter table testtable add age int;
insert into testtable (id, name,age) values(1,'Sam',18),(2,'Tom',20), (3,'Rose',18),(4,'Lisa',20);

1.基本查询

1.1查询多个字段

Select 字段1,字段2... From [表名]

select id,name from testtable;

Select * From [表名]

select * from testtable;

1.2 设置别名

Select 字段1 As 别名1,字段2 As别名2... From [表名]

select id as '编号', name as '姓名', age as '年龄' from testtable;

1.3去除重复记录

insert into testtable (id, name,age) values(1,'Sam',18),(2,'Tom',20), (3,'Rose',18),(4,'Lisa',20);
select * from testtable;

Select Distinct 字段列表 From 表名

select distinct * from testtable;

2.条件查询
创建一个新表用于下面的实操训练,表名为emp,表结构与数据如下图所示。

2.1.语法

Select 字段列表 From 表名 Where 条件列表

2.2条件

2.3实操训练

2.3.1 查询年龄等于88的员工信息

select * from emp where age = 88;

2.3.2 查询年龄小于20的员工信息

select * from emp where age < 20;

2.3.3 查询年龄小于等于20的员工信息

select * from emp where age <= 20;

2.3.4 查询没有身份证号的员工信息

select * from emp where idcard is null;

2.3.5 查询有身份证号的员工信息

select * from emp where idcard is not null;

2.3.6 查询年龄不等于88的员工信息

select * from emp where age <> 88;
select * from emp where age != 88;

2.3.7 查询年龄在15岁(包含)到20岁(包含)之间的员工信息

select * from emp where age between 15 and 20;
select * from emp where age >=15 && age <=20;
select * from emp where age >=15 and age <=20;

2.3.8 查询性别为女且年龄小于25岁的员工信息

select * from emp where gender ='女' and age < 25;

2.3.9 查询年龄等于18或20或40的员工信息

select * from emp where age in (18,20,40);
select * from emp where age = 18 or age = 20 or age = 40;

2.3.10 查询姓名为两个字的员工信息

select * from emp where name like '__';

2.3.11 查询身份证最后一位为X的员工信息

select * from emp where idcard like '%X';

3.聚合函数

3.1介绍

将一列数据作为一个整体,进行纵向计算

3.2常见聚合函数

3.3练习

3.3.1 统计该企业员工数量

select count(*) as '员工数量' from emp;

3.3.2 统计该企业员工的平均年龄

select avg(age) as '平均年龄' from emp;

3.3.3 统计该企业员工的最大年龄

select max(age) as '最大年龄' from emp;

3.3.4 统计该企业员工的最小年龄

select min(age) as '最小年龄' from emp;

3.3.5 统计西安地区员工年龄之和

select sum(age) as '年龄之和' from emp where workaddress = '西安';

4.分组查询

4.1语法

Select 字段列表 From 表名 [Where 条件] Group By 分组字段名 [Having 分组后过滤条件]

4.2 Where 与 Having的区别

·执行时机不同:Where在分组之前,Having在分组之后

·判断条件不同,Where不能对聚合函数进行判断,Having可以

4.3 练习

4.3.1 根据性别分组,统计男性员工和女性员工的数量

select gender,count(*) as '员工数量' from emp group by gender;

4.3.2 根据性别分组,统计男性员工和女性员工的平均年龄

select gender,avg(age) as '平均年龄' from emp group by gender;

4.3.3 查询年龄小于45的员工,并根据工作地址分组,获取员工数量大于等于3的工作地址。

select workaddress as '工作地址' ,count(*) as '员工数量' from emp where age < 45 group by workaddress having count(workaddress) >= 3;

5.排序查询

5.1语法

Select 字段列表 From 表名 Order By 字段1 排序方式1,字段2 排序方式2

5.2 排序方式

·Asc:升序(默认值)

·Desc:降序

5.3 练习

5.3.1 根据年龄对公司的员工进行升序排序

select * from emp order by age asc;
select * from emp order by age;

5.3.2 根据入职时间,对员工进行降序排序

select * from emp order by entrydate desc;

5.3.3 根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序。

select * from emp order by age asc ,entrydate desc;

6.分页查询

6.1语法

Select 字段列表 From 表名 Limit 起始索引,查询记录数;

6.2 解释

·起始索引从0开始,计算起始索引方式为:(查询页码-1)*每页显示记录数

·如果查询的是第一页数据,其实索引可以省略。

6.3 练习

6.3.1 查询第1页员工数据,每页展示10条记录

select * from emp limit 10;

6.3.2 查询第2页员工数据,每页展示10条记录

select * from emp limit 10,10;

7.案例练习

7.1 查询年龄为20,21,22,23岁的员工信息

select * from emp where age in(20,21,22,23);

7.2 查询性别为男,并且年龄在20-40岁(含)以内的姓名为三个字的员工。

select * from emp where gender = '男' and age between 20 and 40 and name like '___';

7.3 统计员工表中,年龄小于60岁,男性员工和女性员工的人数。

select count(*) from emp where age < 60 group by gender;

7.4 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相容按入职时间降序排序。

select name,age from emp where age <= 35 order by age asc, entrydate desc;

7.5查询性别为男,且年龄在20-40岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序。

select * from emp where age between 20 and 40 order by age asc, entrydate asc limit 5;

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言