准备数据如下:
#数据表介绍
#--1.学生表
#Student(SId,Sname,Sage,Ssex)
#--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
#--2.课程表
#Course(CId,Cname,TId)
#--CId 课程编号,Cname 课程名称,TId 教师编号
#--3.教师表
#Teacher(TId,Tname)
#--TId 教师编号,Tname 教师姓名
#--4.成绩表
#SC(SId,CId,score)
#--SId 学生编号,CId 课程编号,score 分数
CREATE TABLE Student(SId VARCHAR(10),Sname VARCHAR(10),Sage DATETIME,Ssex VARCHAR(10));
INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Student VALUES('03' , '孙风' , '1990-12-20' , '男');
INSERT INTO Student VALUES('04' , '李云' , '1990-12-06' , '男');
INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Student VALUES('06' , '吴兰' , '1992-01-01' , '女');
INSERT INTO Student VALUES('07' , '郑竹' , '1989-01-01' , '女');
INSERT INTO Student VALUES('09' , '张三' , '2017-12-20' , '女');
INSERT INTO Student VALUES('10' , '李四' , '2017-12-25' , '女');
INSERT INTO Student VALUES('11' , '李四' , '2012-06-06' , '女');
INSERT INTO Student VALUES('12' , '赵六' , '2013-06-13' , '女');
INSERT INTO Student VALUES('13' , '孙七' , '2014-06-01' , '女');
CREATE TABLE Course(CId VARCHAR(10),Cname NVARCHAR(10),TId VARCHAR(10));
INSERT INTO Course VALUES('01' , '语文' , '02');
INSERT INTO Course VALUES('02' , '数学' , '01');
INSERT INTO Course VALUES('03' , '英语' , '03');
CREATE TABLE Teacher(TId VARCHAR(10),Tname VARCHAR(10));
INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五');
CREATE TABLE SC(SId VARCHAR(10),CId VARCHAR(10),score DECIMAL(18,1));
INSERT INTO SC VALUES('01' , '01' , 80);
INSERT INTO SC VALUES('01' , '02' , 90);
INSERT INTO SC VALUES('01' , '03' , 99);
INSERT INTO SC VALUES('02' , '01' , 70);
INSERT INTO SC VALUES('02' , '02' , 60);
INSERT INTO SC VALUES('02' , '03' , 80);
INSERT INTO SC VALUES('03' , '01' , 80);
INSERT INTO SC VALUES('03' , '02' , 80);
INSERT INTO SC VALUES('03' , '03' , 80);
INSERT INTO SC VALUES('04' , '01' , 50);
INSERT INTO SC VALUES('04' , '02' , 30);
INSERT INTO SC VALUES('04' , '03' , 20);
INSERT INTO SC VALUES('05' , '01' , 76);
INSERT INTO SC VALUES('05' , '02' , 87);
INSERT INTO SC VALUES('06' , '01' , 31);
INSERT INTO SC VALUES('06' , '03' , 34);
INSERT INTO SC VALUES('07' , '02' , 89);
INSERT INTO SC VALUES('07' , '03' , 98);
题目如下:
#39.查询选修了全部课程的学生信息
该题有两种解法:
第一种先查出一共有多少门课程,在查出选修课程数等于课程总数的学生信息:
SELECT student.* FROM sc ,student
WHERE sc.SId=student.SId GROUP BY sc.SId
HAVING COUNT(*) = (SELECT DISTINCT COUNT(*) FROM course )
查询结果如下:
其实还有第二种逻辑使用group_concat函数来拼接课程信息来查询。
最初我的写法如下:
SELECT * FROM student WHERE sid IN(
SELECT sid FROM sc GROUP BY sid HAVING
GROUP_CONCAT(cid)=(
SELECT GROUP_CONCAT(cid) FROM course
))
这个写法查出来的结果跟上面的不一样,结果如下:
仔细检查sc表发现第一种写法查询的结果正确
那第二种方法哪里有问题呢?想了好久最后发现是group_concat拼接内容的顺序问题
改写sql如下:
SELECT * FROM student WHERE sid IN(
SELECT sid FROM sc GROUP BY sid HAVING
GROUP_CONCAT(cid ORDER BY cid ASC)=(
SELECT GROUP_CONCAT(cid ORDER BY cid ASC) FROM course
))
这样查询的结果就没什么问题了,结果如下:
所以以后使用group_cncat来拼接字段当做条件的时候,最好要给拼接的字段排个序