day02
1、排序:order by
升序:select * from 表名 order by 字段1 asc,字段2 asc;(先按字段1排序,若字段1相同则按字段2…)
降序:select * from 表名 order by 字段1 desc,字段2 desc;
例:select * from students order by age desc,studentNo desc;
2、聚合函数 count(*),max(*),min(*),sum(*),avg(*)等
count(*):求某类字段的个数
select count(*) from students where sex='女';
max(*):求某类字段的最大值
select max(age) from students where name like '孙%';
min(*):求某类字段的最小值
sum(*):求某类字段的总和
select sum(age) from students where sex='男';
avg(*):求某类字段的平均值
select avg (age) from students where class='1班';
3、分组:group by
select 字段1,字段2,聚合函数 from 表名 group by 字段1,字段2
select sex,count(*) from students group by sex ;
4、分页:limit 起始,个数(行数从0开始)
select * from students limit 0,3;
5、连接查询——等值连接 查询的结果为两个表匹配到的数据
select * from students, course where students.studentNo=course.studentNo
6、连接查询——内连接
select * from students as stu inner join courses as c on stu.studentNo=c.studentNo;
SELECT stu.name,sc.courseNo,sc.score FROM students as stu INNER JOIN scores as sc on stu.studentsNo=sc.studentno WHERE stu.name='王昭君';