SQL group by,between and,union,having

时间:2024-10-12 00:07:33

1、学生表studentCREATETABLE`student`(`id`int(50)NOTNULLAUTO_INCREMENT,`name际吨孔鳝`varchar(50)DEFAULTNULL,`sex`varchar(48)DEFAULTNULL,`age`varchar(50)DEFAULTNULL,`birthday`varchar(50)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=245DEFAULTCHARSET=utf8

SQL group by,between and,union,having

2、课程表kechengCREATETABLE`kecheng`(螬绣囤扮`id`int(11)NOTNULL荑樊综鲶,`yuwen`int(11)DEFAULTNULL,`shuxue`int(11)DEFAULTNULL,`yinyu`int(11)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8

SQL group by,between and,union,having

3、分组(学生表分组通过班级,查看每班要多少个人)Group bySELECT s.class,COUNT(*) FROM student s GROUP BY s.class

SQL group by,between and,union,having
SQL group by,between and,union,having

4、外连接(学生表和课程表,找到2个表直接id一样的学生)SELECT * FROM student s,kecheng k WHERE s.id=k.id

SQL group by,between and,union,having
SQL group by,between and,union,having
SQL group by,between and,union,having

5、between and(找到年龄在20到40之间的学生)SELECT * FROM student s WHERE s.age BETWEEN 20 AND 40

SQL group by,between and,union,having
SQL group by,between and,union,having

6、having(having和group by 一起用了,可以对分组以后的数据进行处理)(以班级分组,每班的学生的年龄之和小于50)SELECT s.class,COUNT(*),SUM(age) FROM student s GROUP BY s.class HAVING SUM(age)<50

SQL group by,between and,union,having
SQL group by,between and,union,having

7、union(合并的字段必须是相同的了)(合并一下id了)

SQL group by,between and,union,having
SQL group by,between and,union,having
SQL group by,between and,union,having
© 手抄报圈