目录
一、五子句以及执行顺序
select * from 数据表 ①where 条件子句 ②group by 分组子句 ③having 条件子句 ④order by 排序子句 ⑤limit 限制查询子句
若五子句同时出现,必须按照以上顺序
二、使用select 进行简单查询
① select * from 表名 ; #查询所有行所有列
② select 列1 列2 , ... from 表名 ; #查询所有行指定列
③ select 列1 as 别名1 , 列2 as 别名2 , ... from 表名 ; #查询指定列数据并给结果列起别名,实际应用中,as可省略
④ select 表名.列1 , 表名.列2 , ... from 表名 ; #查询所有行指定列
⑤ select 表别名.列1 , 表别名.列2 , ... from 表名 as 表别名 ; #查询表指定列数据并给表起别名
注:AS起别名时,如果别名和关键字相同,要加反引号“ ` ”
三、条件查询
1、基本语句:
select * from 表名 where 条件 ;
2、条件运算符:
(1)比较运算符:= 、> 、< 、>= 、 <= 、!= 、<>(不等于)
(2)逻辑运算符:and 、or 、not
(3)LIKE模糊查询:% 表示任意多个任意字符
_ 表示任意一个字符
(4)范围查询:between...and... 连续的一个范围内查询
in 一个非连续的范围内查询
(5)空值判断:is null 和 is not null
#逻辑查询
select * from students where gender = '男' and age > 20;
select * from students where height =180 or height =185;
select * from students where not (height = 170); #查询学生中身高不为170的所有学生信息
#模糊查询
select * from students where name like '杨%'; #查询姓名以“杨”开头的学生
select * from students where name like '%伟'; #查询姓名以“伟”结尾的学生
select * from students where name like '%小%'; #查询姓名中带有“小”的学生
select * from students where name like '____'; #查询姓名为4个字符的学生
# 范围查询
select * from students where height between 170 and 180; #查询身高在170到180的学生
select * from students where height >= 175 and height <=195;
select * from students where height in (180,190);
select * from students where height=180 or height=190; #均是查询身高在180到190之间的
#空值查询
select * from students where student_id is null; #查询学号为空值地学生
select * from students where student_id is not null; #查询学号不为空值的学生
四、聚合函数
count 求指定列的总记录数
max 求指定列的最大值(maximum)
min 求指定列的最小值(minimum)
sum 求指定列的和
avg 求指定列的平均值(average)
注:聚合函数的计算会忽略null的值
select count(students_id) from students;
#若10行数据,有两行学生id为空值,最终结果为8(忽略null值)
select count(*) from students; #可计算上空值
select count(*) as total_students from students; #统计总记录数并取别名total_students
五、分组查询 group by
#分组聚合:先分组,后聚合
eg:select gender from students group by gender;
#分组+聚合
select gender, count(*) as gender_classify from students group by gender;
#多字段分组+聚合,例:求每门课程男生和女生的数量
eg:
select course, gender from students group by course,gender;
select course,gender,count(*) as total_num from students group by course,gender;
六、Having条件子句
having条件子句是针对group by分组结果进一步筛选,having发生在分组之后,where发生在分组之前。
当无group by分组情况时,having与where效果相同。
select * from students where age >20;
select * from students having age >20;
举个栗子:统计每个课程学生的平均身高,并筛选出平均身高低于170的课程
eg1:select course,avg(height) from studenrts group by course having avg(height)<170;
eg2:select course,avg(height) as avg_height from students group by course having avg_height <170;
七、排序查询order by
① order by 字段 asc => 升序排列(可省略,默认升序)
② order by 字段 desc => 降序排列
#例1:按课程得分对学生信息从高到低排序(降序)
select * from students order by grade desc;
#例2:按学生年龄从小到大排序(升序,可省略)
select * from students order by age;
#注:多字段查询,先按第一个排序,如果能比较出大小,不进行后续排序;如果前面字段相同,则继续按照第二个字段进行比较排序
select * from students order by grade desc,age asc;
八、去重查询 distinct
注:distinct可以对多字段进行去重。但这时(字段1+字段2)是作为一个整体去查询去重的。
(1)查询所有学生姓名,实现去重
select name from students; #查所有
select distinct name from students; #去重
(2)查询所有学生信息(姓名+性别)进行去重操作
select distinct name,gender from students;
九、limit 查询
用于获取查询结果中指定范围内的行。
基本语法:select 字段 from 表名 limit M , N ;
M:开始行索引,默认0,代表从第M+1行开始
N:查询条数,即提取多少条数据
limit N => 若从第0行开始,M可以不写,默认为0
例1:查询年龄最大的学生信息
select * from students order by age desclimit 1;
#例2:
#需求:将学生数据表按年龄从小到大排序,并获取第2页内容(每页3条数据)
#思路:1、排序,按年龄age从小到大排序 2、limit M,N 根据页码和每页条数,计算M和N的值
select * from students order by age limit3,3;