上篇写了DDL和DML地址:https://blog.csdn.net/qq_41589520/article/details/107670279
三.DQL语句
数据表的查询语句;有多种查询方式
1.基本查询
select * from 表名 ; 查询表内所有记录()
select 字段 from 表名 ;查询某个字段
select 字段名 as 别名 from 表名;以别名展示字段内信息不改变字段名
在mysql中取别名是可以不写as 直接 字段名空格别名(只是mysql中)
select distinct 字段名 from 表名;去重查询
2.条件查询
查询符合条件的记录
* select 字段 from 表名 where条件;可以有多个条件
* 基本运算符 =,!=,>, <,都可以使用
* 多个条件使用and连接
* 多个条件符合一个使用or连接
* 查询指定数据内的使用 in 条件
* 查询为null值的 is null
* 不为空的not null
select * from 表名 where id = 1; 查询 id为1的记录
select * from 表名 where id = 1 and name = 'jack'; 查询id为1且name为jack的记录
select * from 表名 where id = 1 or name = 'jack' 查询id为1或name为jack的记录
select * from 表名 where id in (1,2,3);查询id为1,2,3的记录
3.模糊查询
查询一些不能确定的结果
%代表N个字符
_代表一个字符
select * from 表名 where 字段 like %字符%;查询包含字符的所有记录
4.排序
可以和where组合使用,两者没有关联且所有查询不会影响到数据库的存储,只是打印结果的改变
排序规则:
asc:升序,(默认的排序)
desc:降序
select * from order by 字段 ASC ;升序
select * from order by 字段 desc;降序
5.聚合函数
select count(字段) from 表名; 不为空的总数,为null不统计(有多少记录)
select max(字段) from 表名;最大数
select min(字段)from 表名;最小数
select sum(字段)from 表名;总和(字段内所有数据的和)
select avg(字段)from 表名;平均值
6.分组查询
对字段进行分组查询
select 字段名 from 表名 group by 字段;
7.分页查询
select * from 表名 limit 开始的记录索引,每页几条数据;
开始记录索引可以不写 默认为0,
各查询可以组合使用
查询执行的顺序是:
from–>on–>join–>where–>group by–>having–>select–>distinct–>order by–>limit