SQL group by,between and,union,having

2025-10-10 02:20:15

1、学生表student

CREATE TABLE `student` (  `id` int(50) NOT NULL AUTO_INCREMENT,  `name` varchar(50) DEFAULT NULL,  `sex` varchar(48) DEFAULT NULL,  `age` varchar(50) DEFAULT NULL,  `birthday` varchar(50) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=245 DEFAULT CHARSET=utf8

SQL group by,between and,union,having

2、课程表kecheng

CREATE TABLE `kecheng` (  `id` int(11) NOT NULL,  `yuwen` int(11) DEFAULT NULL,  `shuxue` int(11) DEFAULT NULL,  `yinyu` int(11) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

SQL group by,between and,union,having

3、分组(学生表分组通过班级,查看每班要多少个人)Group by

SELECT 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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢