DQL
数据执行DQL的时候,不会对数据进行更改,只是把服务端的数据拿出来给客户端,查询的结果是一张表
基础查询
查询数据 select
语法:select 显示的字段from 表名 where 条件
查询所有列 *代表所有列
select *from 表名;
查询指定列
select 字段1、字段2、字段3 from 表名;
条件查询 where
where后面再加上一些运算符
关系运算符:= != > >= < <=
区间:between A and B ;[A,B]
and:和
or:或
not:非
is null:空
is not null:非空
in:在什么里面,包含
模糊查询
当想查询的名字中包含A的时候、就需要模糊查询了
通配符
_:表示任意一个
%:表示0~X任意个字符
select 字段名 from 表名 where 字段名 like “通配符”
字段控制
去除重复项 select distinct
select distinct 字段名 form 表名
别名 as
select 字段名1 as 别名1 字段名2 别名2 from 表名
//可以使用as或者空格
排序 order by
select 字段名 from 表名 order by 字段名 asc
//按照字段名排序,asc升序,desc降序,不写默认升序
多关键字排序
select 字段名from 表名 order by 字段名1 desc ,字段名2 asc
//第一个出现为主关键字,其他为副关键字
聚合函数
count() 计数
select count(字段)from 表名
max() 最大值
select max(字段)from 表名
min() 最小值
select min(字段)from 表名
sum() 求和
select sum(字段)from 表名
avg() 平均值
select avg(字段)from 表名
round() 四舍五入
select round(字段)from 表名