用来查询数据库中的数据

  1. 查询所有的列
  1. select * from 表名;
  1. 结果集
    • 数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端
    • 结果集
      • 通过查询语句查询出来的数据以表的形式展示我们称这个表为虚拟结果集,存放在内存中
  2. DQL
    • 条件查询 ```javascript //1. 查询制定列的数据 select 列名1,列名2 …. from 表名

// 2. 查询条件 关键字 where

a. =(等于) !=(不等于) <>(不等于) <(小于) <=(小于等于) >(大于) >=(大于等于) b. between … and; 值在什么范围之内 c. in(set) 固定的范围值 d. is null; (为空) is not null(不为空) e. and; 与 条件都要成立 f. or; 或 有一个条件成立就行 g. not; 非

// 3. 题目 查询性别为男,并且年龄为20的学生 select * from students where age=20 and sex=’男’;

查询学号为1001 或者名字为张三的记录 select * from students where id=1001 or name=’张三’;

查询学号为1001 1002 1003 的学生 select from students where id=1001 or id=1002 or id=1003; select from students where id between 1001 and 1003; // id 在1001 和 1003 之间的 select * from students where id in(1001,1002,1003); // 轮询查找

查询年龄为null的记录 select * from students where age=null;

查询年龄18到20之间的 select from students where age between 18 and 20; // age 18 和 20 之间的 select from students where age>=18 and age<=20; // age 18 和 20 之间的

查询性别非男 select * from students where sex !=’男’;

  1. - 模糊查询
  2. ```javascript
  3. // 1. 根据指定的关键进行查询
  4. // 2. 使用like 关键字后跟通配符
  5. // 3. 通配符 下划线 _ 标识任意一个字符
  6. // 百分号 % 表示0到n个字符
  7. a. 查询姓名由五个字母构成
  8. select * from students where name like '_____'
  9. b. 查询姓名由五个字母构成 并且第五个为's'的学生记录
  10. select * from students where name like '____s'
  11. c. 查询姓名以m开头的学生记录
  12. select * from students where name like 'm%'
  13. d. 查询姓名第二个字母为u的
  14. select * from students where name like '_u%'
  15. e. 查询姓名包含字母s的学生记录
  16. select * from students where name like '%s%' // 无论是第一个字母还是中间的或者是最后的 只要包含s都会被搜索到
  • 字段控制查询 ```javascript //1. 字段去重 distinct select distinct stu_name from student;

//2. 组合字段 产生一个新列名 和 列值 不会更新到数据库 返回的是一个查询副本 select age, score, age+score from student // age+score 为新列名 select age, score, ifnull(age, 1)+ifnull(score,0) from student; // 如果当前记录age为null 则使用默认值1 同理 score 使用默认值0 ifnull(age, 1)+ifnull(score,0) 为新列名

// 如果列名太长了 可以使用as 给其起别名 // 可以用于后端给前端返回数据的时候,一个字段不在数据库中,是以这样处理的方式返回给前端的新的字段名 select age, score, ifnull(age, 1)+ifnull(score,0) as nnnn from student;

  1. - 排序
  2. ```javascript
  3. // 关键字段 order by
  4. // 升序 asc
  5. select * from student order by age asc; // 学生的年龄以从小到大的顺序进行排列
  6. // 降序 desc
  7. select * from student order by age desc;
  8. // 题目 将学生以年龄顺序 降序排列 如果年龄一样就以编号排列
  9. select * from student order by age desc, id asc; // 先以年龄降序排列,年龄相同的 以id的升序排列
  • 聚合函数 ```javascript // 1. 对查询的结果进行统计

// 2. 常用聚合函数 a. count():统计指定列不为null的记录行数 b. max(): 计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算 c. min(): 计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算
d. sum(): 计算指定列的数值和,如果指定列不是数值类型,那么计算结果为0 e. avg(): 计算指定列的平均值,如果指定列不是数值类型,那么计算结果为0

//3. 使用 // 查询表中有多少条记录 select count(*) from students;

  1. //查询某一列数据不为null的行数
  2. select count(age) from student;
  3. //查询age数据不为null的行数且大于10
  4. select count(age) from student where age > 10;
  5. //同时查询多个列不为null的记录行数
  6. select count(age), count(name) from students;
  7. //查询某一列求和
  8. select sum(age) from students;
  9. // 某一列平均值
  10. select avg(age) from students;
  11. // 某一列最小值
  12. select min(age) from students;
  13. // 某一列最大值
  14. select max(age) from students;
  1. - 分组查询
  2. ```javascript
  3. // 1. 什么是分组 将查询结果按照1个或多个字段进行分组,字段值相同的为一组
  4. // 2. 进行分组 关键字 group by
  5. select * from student group by sex; // 以性别进行分组
  6. // 将数据按性别分组 然后拿到对应组中所有的姓名
  7. select group_concat(name) from student group by sex; // 查询性别分组中 每一组内所有的名称
  8. // 我可以先把数据分组 然后在组内的数据进行聚合函数的操作,求和,求最小值等,组内操作
  9. // 没有做分组的话 聚合函数查的都是所有的数据的情况 而不是组内
  10. // 查询男女分组中 年龄最小的
  11. select group_concat(age), min(age) from student group by sex;
  12. // 查询男的有多少 女的有多少
  13. select group_concat(name), count(*) from student group by sex;
  14. // 关键字 having 必须用到group by 之后 对分组之后的数据在进行一个处理
  15. select group_concat(mony), count(*) from student group by sex having sum(mony) > 9000;
  16. # having 和 where 作用是一样的, having 只能用于group by 之后 别的不可以, where 是哪里都可以
  17. # having 是在分组之后对数据进行过滤
  18. # where 是分组之前对数据进行过滤 先执行
  19. # having 后可以使用分组函数 where 不可以使用分组函数
  20. # where先执行,不满足where的数据 是不参加分组的
  • limit ```javascript // limit 后面跟两个参数 从哪个位置开始 和 取多少条 慎用 select * from student limit 0,3 // 从角标为0的开始 查三个数据

现将所有的数据查出来,然后定位到具体的位置,将前面的都废弃

  1. - select 顺序
  2. ```javascript
  3. // 书写顺序 从左到右
  4. select ----> from -----> where -----> group by -----> having -----> order by -----> limit
  5. // 执行顺序
  6. form -----> where ------> group by ------> having ------> select -----> order by -----> limit