带条件的查询
-- 与的关系 and、between...and
-- 查询年龄在18到20之间的所有学生
select * from student where age>=18 and age<=20;
select * from student where age between 18 and 20;
-- 不等于 !=、<>
--或 or、in
-- 不在里面not in
-- 查询编号不是1003和1005的学生信息
select * from student where id not in(1003,1005);
-- 模糊查找 like
-- 通配符:%(表示任意个任意字符) 、 _(任意一个字符)
-- null表示没有数据
-- 查询没有缺考的学生信息
select * from student where score is not null;
select * from student where not score is null;
select子句
可以在子句中进行运算
使用distinct去除重复行数据
起别名
-- null不参与运算
select name,score,score+10 from student;
--起别名,使用as关键字,可以省略
select name,score,score+10 as new_score from student;
-- ifnull函数,类似于java中的三目运算符,用于处理null,如果某字段为null,会使用给定值进行替换
select name,score,IFNULL(score,0)+10 new_score from student;
-- 查询所有学生的年龄去掉重复行
select distinct age from student;
排序
ASC,升序,ascending,默认为升序
DESC,降序,descending
-- 查询所有学生信息, 按照id进行升序排序
select *from stundet order by id asc;
-- 查看年龄为20的学生信息, 按照id降序排序
select * from student where age=20 order by id desc;
-- 可以按照别名进行排序
-- 查询所有学生的新成绩, 并按照新成绩排名
select name, score+10 new_score from student order by new_score desc;
-- order by后可以写数字, 代表第几列, 一般不用
select * from student order by 6;
-- rand()函数, 生成随机数, 范围是[0,1)
-- 配合rand函数可以进行数据的随机排序, 打乱数据, 洗牌
select * from student order by rand();
-- 查询所有学生的信息, 按照年龄降序排序, 如果年龄相同, 再按照id降序排序
select * from student order by age desc, id desc;
分组函数
count()、sum()、max()、min()、avg()
-- 统计所有学生的人数
select count(*) from student;
-- 查询总分,最低分和最高分数以及平均分数
select sum(score),min(score),max(score),avg(score) from student;
分组查询
在这里插入代码片