-- 单表查询-- 查询所有字段select id ,name,height,habit from t_student
select * from t_student -- 两者的作用一样
-- 查询指定字段
select id,name from t_student
-- Where 条件查询
select * from t_student where id =2 -- 查询id=2
-- In 关键字查询
select * from t_student where age in (20,22)-- 包含2022的
-- 带between and 的范围查询
select * from t_student WHere age between 20and22 -- 查询范围在20 到22的
-- notin 和not between 表达不包含 和不再范围之内
-- LIKE 模糊查询
-- '%'表示任意字符
-- ‘_’表示单个字符
selECT * from T_student where name like'dzb'selECT * from T_student where name like'%dzb' -- DZB 前面是任意字符
selECT * from T_student where name like'%dzb%' -- 只要包含DZB
selECT * from T_student where name like'dzb%' -- DZB后面是任意字符
-- 空值查询
select * from T_student where sex isnull;-- sex字段为空-- 带AND多条件查询select * from t_student where age=22and sex='male'
-- 带OR的 多条件查询
select * from t_student where age=22or sex='male'
-- DISTINCT 去重复查询
selectDISTINCT name from t_student
-- 对查询结果排序 ORDERBYselect * from t_student ORDERBY age ASC-- 默认为升序
select * from t_student ORDERBY age DESC -- 降序
-- 分组查询 GROUPBY
-- 单独使用没有意义
-- 一般和聚合函数一起使用 COUNT
-- 与having一起使用 --限制输出结果
select sex ,GROUP_CONCAT(NAME) from t_student GROUPBY sex -- 按照性别吧查询到的name分组
select sex ,COUNT(NAME) from t_student GROUPBY sex -- 统计性别总人数
select sex ,COUNT(NAME) from t_student GROUPBY sex havingCOUNT(NAME)>=2 -- 限制性别总人数大于2select sex ,COUNT(NAME) from t_student GROUPBY sex WITH ROLLUP-- =多加一行统计性别总人数
-- LIMIT分业查询
select * from t_student LIMIT 0,3 -- 查询0到3条
第四部分 聚合函数查询
-- COUNT(expr)查询记录总条数-- 与group by 一起使用selectCOUNT(*) from t_score
select name, COUNT(*) from t_score GROUPBY`name`
-- SUM(expr)函数
-- 1.求和函数
-- 2.与groupby一起使用
select name, SUM(score) from t_score WHERE name='张三'select name, SUM(score) from t_score GROUPBY`name`
--AVG([DISTINCT] expr)求平均值
select name, avg(score) from t_score GROUPBY`name`
-- MAX(expr)求最大值
select name,subject, max(score) from t_score WHERE name='张三'select name, max(score) from t_score GROUPBY`name`
-- MIN(expr)求最小值