DQL 数据库查询语言【查询数据库中的数据】
目录
三、聚合函数【count()、max()、min()、sum()、avg()】
格式
select 字段列表|*
from 表名
[where 搜索条件]
[group by 分组字段 [having 分组条件]]
[order by 排序字段 排序规则]
[limit 分页参数];
一、基本查询
-- 1)查询所有列
select * from 表名;
select * from emp;
-- > * 表示查询所有列
-- 2)查询指定列
select 列1,(列2,.....) from 表名;
select empno,ename,sal from emp;
distinct 去重
当查询结果中的多行记录一模一样时,只显示一行。一般查询所有列时很少有这种情况,但只查询一列(或几列)时,这种可能就大了
select distinct salary from employee;
-- 查询员工表的工资时,如果存在相同的工资只显示一次
加减乘除(+ - * /)列运算
select salary * 1.5 from employee;
select salary + overtime from employee;
concat 查询结果拼接
select concat('姓名:',name,',部门:',depid) from employee;
+---------------------------------------+
| concat('姓名:',name,',部门:',depid) |
+---------------------------------------+
| 姓名:张三,部门:111 |
| 姓名:李四,部门:111 |
+---------------------------------------+
ifnull 转换NULL值
如salary(salary = null)+ 10000中,salary为 null 值时运算结果还是 null 。这就需要把 null 转换成其他值
select ifnull(salary,700)+1000 from employee;
-- > ifnull(salary,700):如果salary中存在null值,则换成700来运算
as 给列起别名
select ifnull(salary,700)+1000 from employee;
+-------------------------+
| ifnull(salary,700)+1000 |
+-------------------------+
| 3000 |
+-------------------------+
-- 格式
select 列运算 as 别名 from 表名;
select (ifnull(salary,700)+1000) as '工资' from employee;
+------+
| 工资 |
+------+
| 3000 |
+------+
where 条件查询
和update和delete一样,select也可以使用where来进行数据的过滤
-- 查询users表中 age > 22的数据
select * from users where age > 22;
-- 查询 users 表中 name=某个条件值 的数据
select * from users where name = '王五';
-- 查询 users 表中 年龄在22到25之间的数据
select * from users where age >= 22 and age <= 25;
select * from users where age between 22 and 25;
-- 查询 users 表中 年龄不在22到25之间的数据
select * from users where age < 22 or age > 25;
select * from users where age not between 22 and 25;
-- 查询 users 表中 年龄在22到25之间的女生信息
select * from users where age >= 22 and age <= 25 and sex = '女';
and 和 or 使用注意
and 的优先级高于 or
-- 查询 users 表中 年龄为22或者25 的女生信息
select * from users where age=22 or age = 25 and sex = '女';
-- 查询变成了为年龄22的不管性别,或者年龄为 25的女生
-- 正确的方法:使用小括号来关联相同的条件
select * from users where (age=22 or age = 25) and sex = '女';
like 模糊查询
-- like 语句 like某个确定的值 和。where name = '王五' 是一样
select * from users where name like '王五';
-- 使用 % 模糊搜索。%代表0~N个任意字符
-- 查询name字段中包含五的数据
select * from users where name like '%五%';
-- 查询name字段中最后一个字符 为 五的
select * from users where name like '%五';
-- 查询name字段中第一个字符 为 王 的
select * from users where name like '王%';
-- 使用 _ (单个的下划线)。表示一个任意字符
-- 查询表中 name 字段为两个字符的数据
select * from users where name like '__';
-- 查询 name 字段最后为五,的两个字符的数据
select * from users where name like '_五';
注意:where 子句中的 like 在使用 % 或者 _ 进行模糊搜索时,效率不高
·尽可能的不去使用 % 或者 _
·如果需要使用,也尽可能不用把通配符放在开头处
二、order by 排序
asc 升序,默认 可以忽略不写
desc 降序 不可以忽略不写
-- 按照年龄对结果进行排序,从大到小(降序)
select * from users order by age desc;
-- 从小到大排序(升序) asc 默认就是。可以不写
select * from users order by age;
-- 也可以按照多个字段进行排序
select * from users order by age,id;
-- 先按照age进行升序排序,age相同情况下,按照id进行降序排序
select * from users order by age,id desc;
三、聚合函数【count()、max()、min()、sum()、avg()】
-- 计算 users 表中 最大年龄,最小年龄,年龄和及平均年龄
select max(age),min(age),sum(age),avg(age) from users;
-- 统计 users 表中的数据量
select count(*) from users;
select count(id) from users;
-- count(*) 是按照 users表中所有的列进行数据的统计,只要其中一列上有数据,就可以计算
-- count(id) 是按照指定的 (id) 字段进行统计
-- 注意:如果指定的列上出现了NULL值,那么为NULL的这个数据不会被统计
四、group by 分组查询
-- 统计 users 表中 男女生人数,
-- 很明显按照上面的需要,可以写出两个语句进行分别统计
select count(*) from users where sex = '女';
select count(*) from users where sex = '男';
-- 可以使用分组进行统计,更方便
select sex,count(*) from users group by sex;
-- Having 子句 在分组后进行条件过滤(where是分组前的条件过滤)
-- 统计班级人数,并且要人数达到5人及以上
select classid,count(*) as num from users group by classid having num >=5;
五、limit 数据分页(MySQL方言)
-- 查询users表中的数据,只要3条
select * from users limit 3;
-- 跳过前4条数据,再取3条数据
select * from users limit 4,3;
-- limit一般应用在数据分页上面
-- 例如每页显示10条数据,第三页的 limit应该怎么写? 思考
第一页 limit 0,10
第二页 limit 10,10
第三页 limit 20,10
第四页 limit 30,10