基础语句
基础的查询
1.查询name,age
Select name,age from student;
Select * from students;//不建议使用,会导致运行时间太长,以及代码不易维护
2.查询中遇到重复的
Select distinct name from students
3.查询中重新起名字更容易看清as可以省略,别名之间有一个空格隔开
Select name,math as 数学成绩 ,english as 英语成绩 from students
条件查询
1.查询年纪大于等于20岁的学员信息
Select * from stu where age>=20;
2.查询年纪大于等于20小于等于30
Select * from stu where age>=20 and age<=30
Select * fro stu whew between 20 and 30
3.查询年纪等于20的学员信息
Select * from stu where age=20;
Select * from stu where age!=20;
Select * from stu where <>age;
4.查询年纪等于18或者南极等于20
Select * from stu age in(18,20);
Select * from stu age =18 or age=20;
5.查询英语成绩为空的学员信息
Select * from stu where english is null;
6.查询姓马的学员信息
Select * from stu where name like ‘马%’;
7.查询第二个字的花的学员
Select * from stu where namae like ‘_花%’
8.查询名字中包含明的学员信息
Select * from stu where name like ‘%明%’
##排序查询
1.学员年龄按照年纪升序排列
Select * from stu order by age asc
2.学员年龄安好年龄降序排列
Select * from stu order by age desc
3.查询学员信息,按照数学成绩降序排列,如果数学成绩一样的话,使用英语升序排列
Select * from stu order by math desc, english asc;
聚合函数
avg()平均分,max()最大值,min()最小值,count()统计数量,sum()求和,
1.统计班级一共多少人
Select count(id) from stu;
Select count(*) from stu;
2.统计数学成绩最高的
Select max(math_score) from stu;
3.查询数学成绩的最低分
Select min(math_score) from stu;
4.查询数学成绩的总分
Select sum(math_score) from stu;
##分组查询(分组字段)
1.查询男同学和女同学各自数学平均数
Select sex,avg(math_score) from stu group by sex;
2.查询男同学和女同学各自的分数平均分,以及总人数(各自),要求:分数低于70不参与
Select sex,avg(math),count(*) from stu where math>70 group by sex;
3.查询男同学和女同学各自的分数平均分,以及总人数(各自),要求:分数低于70不参与,分组之后人数大于having
执行顺序 where >聚合类函数>having
## 分页查询
```sql
1.查询0-3条
Select name from stu limit 0,3
2.每页显示三条,查询第一页数据
Select name from stu limit 0,3
3.每页显示三条,查询第二页数据
Select name from stu limit 3,3;
每页总数是相同的,动态索引数为=(当前页码-1)*每页显示的条数