橙色网站模版,长沙公司网络营销推广,建个公司网站需要多少钱,农村电商网站建设计划书目录
1. DQL概念
2. DQL - 编写顺序
3. 基础查询
3.1 查询多个字段
3.2 字段设置别名
3.3 去除重复记录
3.4 案例
4. 条件查询
4.1 语法
4.2 条件
4.3 案例#xff1a;
5. 聚合函数
5.1 常见的聚合函数#xff1a;
5.2 语法
5.3 案例#xff1a;
6. 分组查…目录
1. DQL概念
2. DQL - 编写顺序
3. 基础查询
3.1 查询多个字段
3.2 字段设置别名
3.3 去除重复记录
3.4 案例
4. 条件查询
4.1 语法
4.2 条件
4.3 案例
5. 聚合函数
5.1 常见的聚合函数
5.2 语法
5.3 案例
6. 分组查询
6.1 语法
6.2 案例
7. 排序查询
7.1 语法
7.2 排序方式
7.3 案例
8. 分页查询
8.1 语法
8.2 案例
9. 执行顺序
1. DQL概念
DQL英文全称是Data Query Language(数据查询语言)数据查询语言用来查询数据库中表的记录。
查询关键字: select
在一个正常的业务系统中查询操作的频次是要远高于增删改的当我们去访问企业官网、电商网站在这些网站中我们所看到的数据实际都是需要从数据库中查询并展示的。而且在查询的过程中可能还会涉及到条件、排序、分页等操作。
2. DQL - 编写顺序 select 字段列表 from 表名列表 where 条件列表 group BY 分组字段列表 having 分组后条件列表 order BY 排序字段列表 limit 分页参数 3. 基础查询
在基本查询的DQL语句中不带任何的查询条件查询的语法如下:
3.1 查询多个字段 select 字段1, 字段2, 字段3 ... from 表名 ;select * from 表名 ; 注意 : * 号代表查询所有字段在实际开发中尽量少用不直观、影响效率。
3.2 字段设置别名 select 字段1 [ as 别名1 ] , 字段2 [ as 别名2 ] ... from 表名;select 字段1 [ 别名1 ] , 字段2 [ 别名2 ] ... from 表名; 3.3 去除重复记录 select distinct 字段列表 from 表名; 3.4 案例
-- ------------------------------------------------------------------基础查询
# 查询name, workno, age
select name, workno, age from emp;# 查询所有数据
select * from emp;# 将ID3的名字修改为张飞
update emp set name 张飞 where id 3;# 查询workaddress
select workaddress from emp;# 修改别名
select workaddress as 工作地址 from emp;
select workaddress 工作地址 from emp;# 去重
select distinct workaddress 工作地址 from emp;# 修改表中wordaddress为workaddress
alter table emp change wordaddress workaddress varchar(50) comment 工作地址;4. 条件查询
4.1 语法 select 字段列表 from 表名 where 条件列表 ; 4.2 条件
常用的比较运算符如下:
比较运算符功能大于大于等于小于小于等于等于 或 !不等于between... and...在某个范围之内(含最小、最大值)in(...)在in之后的列表中的值多选一like占位符模糊匹配(_匹配单个字符, %匹配任意个字符)is NULL是NULL
常用的逻辑运算符如下:
逻辑运算符功能and 或 并且 (多个条件同时成立)or 或 ||或者 (多个条件任意一个成立)not 或 !非 , 不是
4.3 案例
-- ------------------------------------------------------------------条件查询
# 查询年龄不等于18
select * from emp where age ! 18;
select * from emp where age 18;# 查询年龄小于40
select * from emp where age 40;# 查询idcard不空值的数据
select * from emp where idcard is not null;# 查询idcard是空值的数据
select * from emp where idcard is null;# 设置16行idcard为空值
update emp set idcard null where id 16;
update emp set idcard 12345678998765432x where id 13;# 查询年龄在30到50之间
select * from emp where age 30 age 50;
select * from emp where age 30 and age 50;
select * from emp where age between 30 and 50;# between接最小值and接最大值
select * from emp where age between 50 and 30;# 查询性别女且在蜀国的数据
select * from emp where gender 女 and workaddress 蜀国;# 查询年龄等于18或等于30或等于40
select * from emp where age 18 or age 30 or age 40;
select * from emp where age in(18, 30, 40);# 查询名字为三个字的名字
select * from emp where name like ___;# 查找idcard的最后一位为x
select * from emp where idcard like %X;
5. 聚合函数
介绍将一列数据作为一个整体进行纵向计算 。
5.1 常见的聚合函数
函数功能count统计数量max最大值min最小值avg平均值sum求和
5.2 语法 select 聚合函数(字段列表) from 表名 ; 注意 : NULL值是不参与所有聚合函数运算的。
5.3 案例
-- ------------------------------------------------------------------聚合函数
# 统计所有数量
select count(*) from emp;
select count(id) from emp;# 统计所有人平均年龄
select avg(age) from emp;# 统计最大年龄
select max(age) from emp;# 统计最小年龄
select min(age) from emp;# 统计蜀国将领人员的年龄之和
select sum(age) from emp where workaddress 蜀国;
6. 分组查询
6.1 语法 select 字段列表 from 表名 [ where 条件 ] group by 分组字段名 [ having 分组后过滤条件 ]; where与having区别
执行时机不同where是分组之前进行过滤不满足where条件不参与分组而having是分组之后对结果进行过滤。
判断条件不同where不能对聚合函数进行判断而having可以。
6.2 案例
-- ------------------------------------------------------------------分组查询
# 根据性别分组统计男性和女性的分组
select gender, count(*) from emp group by gender;# 根据性别分组统计男性员工和女性员工的平均年龄
select gender, avg(age) from emp group by gender;# 查询年龄小于45的人员数量并且根据工作地址分组获取人员数量大于等于3的工作地址
select workaddress, count(*) from emp where age 45 group by workaddress having count(*) 3;
# 起别名
select workaddress, count(*) address_count from emp where age 45 group by workaddress having address_count 3;注意事项:
分组之后查询的字段一般为聚合函数和分组字段查询其他字段无任何意义。执行顺序: where 聚合函数 having 。支持多字段分组, 具体语法为 : group by columnA,columnB
7. 排序查询
7.1 语法 select 字段列表 from 表名 order by 字段1 排序方式1 , 字段2 排序方式2 ; 7.2 排序方式 ASC : 升序(默认值)DESC: 降序 7.3 案例
-- ------------------------------------------------------------------排序查询
# 根据年龄升序排序
select * from emp order by age asc;
# asc可省略
select * from emp order by age;# 根据年龄升序降序
select * from emp order by age desc;# 修改入职时间
update emp set entrydate 1990-02-03 where id 4;
update emp set entrydate 1993-12-03 where id 6;
update emp set entrydate 1993-05-03 where id 8;
update emp set entrydate 1909-02-03 where id 11;# 根据入职时间做降序排序
select * from emp order by entrydate desc;# 根据年龄升序年龄相同再按照入职时间降序排序
select * from emp order by age, entrydate desc;
注意事项
如果是升序, 可以不指定排序方式ASC ;如果是多字段排序当第一个字段值相同时才会根据第二个字段进行排序 ;
8. 分页查询
8.1 语法 select 字段列表 from 表名 limit 起始索引, 查询记录数 ; 8.2 案例
-- ------------------------------------------------------------------分页查询
# 查询第1页人员数据根据展示5条记录
select * from emp limit 0, 5;
# 查询首页0可以省略
select * from emp limit 5;# 查询第3页人员数据根据展示5条记录(页码-1*页码展示记录数)
select * from emp limit 10,5;
注意事项:
起始索引从0开始起始索引 查询页码 - 1* 每页显示记录数。分页查询是数据库的方言不同的数据库有不同的实现MySQL中是limit。如果查询的是第一页数据起始索引可以省略直接简写为 limit 10。
9. 执行顺序