【MySQL学习笔记004】SQL语法与分类—DQL

本文介绍了DQL(数据查询语言)的核心概念,包括基本查询、条件查询、分组查询、排序和分页查询。详细讲解了各种查询指令,如WHERE、GROUPBY、ORDERBY和LIMIT等,并提供了多个示例来说明如何使用这些指令进行数据库操作。此外,还涵盖了聚合函数的使用,如COUNT、MAX、MIN、AVG和SUM。
摘要由CSDN通过智能技术生成

1、DQL

1.1 定义

数据查询语言(Data Query Language,DQL):用来查询数据库中数据表的数据。

1.2 核心指令

DQL操作主要包括条件、分组、排序、分页查询,具体如下:
(1)条件查询:WHERE

(2)分组查询:GROUP BY 分组字段列表 HAVING 分组后条件列表

(3)排序查询:ORDER BY

(4)分页查询:LIMIT

1.3 DQL-数据库操作命令

1.3.1 基本查询

(1)命令编写顺序

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

(2)命令执行顺序

from 表名列表 ->where 条件列表 -> group by 分组字段列表 -> having 分组后条件列表 -> select 字段列表 ->order by 排序字段列表 -> limit 分页参数

(3)查询多个字段

select 字段1,字段2… from 表名;
select * from 表名; ( * 表示返回所有字段)

(4)设置别名

select 字段1 [as] 别名1, 字段2 [as] 别名2 … from 表名;

(5)去除重复记录

select distinct 字段列表 from 表名;

(6)例(练习数据表见最后 附)

  1. 查询指定字段name, worknumber,age返回

select name, worknumber, age from emp;

  1. 查询所有字段返回

select id, worknumber, name, gender, age, idcard, workaddress, entrydate from emp;
select * from emp;

  1. 查询所有员工的工作地址,起别名

select workaddress as ‘工作地址’ from emp;
select workaddress ‘工作地址’ from emp;

  1. 查询公司员工的上班地址(不要重复)

select distinct workaddress from emp;

1.3.2 条件查询

(1)语法

select 字段列表 from 表名 where 条件列表;

(2)常用运算符

比较运算符功能
>大于
>=大于等于
<小于
<=小于等于
=等于
<> or !=不等于
between…and…在某个范围之内
in(…)在in之后的列表中的值,多选
like 占位符模糊匹配(_匹配单个字符, %匹配任意个字符)
is null是null
逻辑运算符功能
and 或 &&并且(多个条件同时成立)
or 或 II或者(多个条件任意一个成立)
not 或 !非,不是

(3)例

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

select * from emp where age = 88;

  1. 查询年龄小于28的员工信息

select * from emp where age < 20;

  1. 查询年龄小于等于20岁的员工信息

select * from emp where age<= 20;

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

select * from emp where idcard is not null;

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

select * from emp where idcard is not null;

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

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

  1. 查询年龄在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;

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

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

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

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

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

select * from emp where name like ‘__’;

  1. 查询身份证号最后一位是X的员工信息

select * from emp where idcard like ‘%X’;

1.3.3 分组查询

(1)语法

select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];

where与having 区别

a. 执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤

b. 判断条件不同:where不能对聚合函数进行判断,而having可以。

注意:

a. 执行顺序:where > 聚合函数 > having;

b. 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义

(2)例

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

select gender, count(*) from emp group by gender;

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

select gender, avg(age) from emp group by gender;

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

select workaddress, count(*) address_number from emp where age < 45 group by workaddress having address_number >= 3;

1.3.4 排序查询

(1)语法

select 字段列表 from 表名 order by 字段1 排序方式1, 字段2 排序方式2;

排序方式

a. ASC:升序(默认值,可省略)

b. DESC:降序

注意:

如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序。

(2)例

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

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

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

select * from emp order by entrydate desc;

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

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

1.3.5 分页查询

(1)语法

select 字段列表 from 表名 limit 起始索引, 查询记录数;

注意:

a. 起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数。

b. 分页查询是数据库的方言,不同数据库有不同的视线,MySQL中是limit;

c. 如果查询的是第一页数据,起始索引可以省略,直接简写为limit 10

(2)例

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

select * from emp limit 0, 10;
selcet * from emp limit 10;

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

select * from emp limit 10, 10 ;

1.3.6 聚合函数

在DQL语句中,常用到聚合函数进行相关计算,常见的聚合函数如下:

函数功能
count统计数量
max最大值
min最小值
avg平均值
sum求和

(1)语法

select 聚合函数(字段列表) from 表名;

注意:

null值不参与所有聚合函数运算

(2)例

  1. 统计该企业员工数量

select count(*) from emp;

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

select avg(age) from emp;

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

select max(age) from emp;

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

select min(age) from emp;

  1. 统计西安地区员工的年龄之和

select sum(age) from emp where workaddress = ‘西安’;

1.4 DQL语句联系

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

select * from emp where gender = ‘女’ and age in (20, 21, 22, 23);

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

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

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

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

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

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

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

select * from emp where gender = ‘男’ and age between 20 and 40 order by age asc, entrydate asc limit 0, 5;

  1. 查询年龄大于15的员工姓名、年龄,并根据年龄进行升序排序

select name, age from emp where age>15 order by age;

附:练习数据表

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

insert into emp (id, worknumber, name, gender, age, idcard, workaddress, entrydate) values
(1,‘1’,‘柳岩’,‘女’,20,‘123456789012345678’,‘北京’,‘2000-01-01’),
(2,‘2’,‘张无忌’,‘男’,18,‘123456789012345670’,‘北京’,‘2005-09-01’),
(3,‘3’,‘韦一笑’,‘男’,38,‘123456789712345670’,‘上海’,‘2005-08-01’),
(4,‘4’,‘赵敏’,‘女’,18,‘123456757123845670’,‘北京’,‘2009-12-01’),
(5,‘5’,‘小昭’,‘女’,16,‘123456769012345678’,‘上海’,‘2007-07-01’),
(6,‘6’,‘杨道’,‘男’,28,‘12345678931234567X’,‘北京’,‘2006-01-01’),
(7,‘7’,‘范瑶’, ‘男’,40,‘123456789212345670’,‘北京’,‘2005-05-01’),
(8,‘8’,‘黛绮丝’,‘女’,38,‘123456157123645670’,‘天津’,‘2015-05-01’),
(9,‘9’,‘范凉凉’,‘女’,45,‘123156789012345678’,‘北京’,‘2010-04-01’),
(10,‘10’,‘陈友谅’,‘男’,53,‘123456789012345670’,‘上海’ ,‘2011-01-01’),
(11,‘11’,‘张士诚’,‘男’,55,‘123567897123465670’,‘江苏’,‘2015-05-01’),
(12,‘12’,‘常遇春’,‘男’,32,‘123446757152345670’,‘北京’,‘2004-02-01’),
(13,‘13’,‘张三丰’,‘男’,88,‘123656789012345678’,‘江苏’,‘2020-11-01’),
(14,‘14’,‘灭绝’,‘女’,65,‘123456719012345670’,‘西安’,‘2019-05-01’),
(15,‘15’,‘胡青牛’,‘男’,70,‘12345674971234567X’,‘西安’,‘2018-04-01’),
(16,‘16’,‘周芷若’,‘女’,18,null,‘北京’,‘2012-06-01’);

————————————————
以上内容仅为学习笔记,若有问题,请大家指正批评!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值