我想查询sid =“02”学生学习同一门课程的学生sid;

时间:2016-04-01 13:27:21

标签: mysql database

    CREATE TABLE `student` (
     `sid` varchar(5) NOT NULL DEFAULT '',
     `sname` varchar(10) NOT NULL,
     `age` date DEFAULT NULL,
     `sex` varchar(2) DEFAULT NULL,
     PRIMARY KEY (`sid`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    CREATE TABLE `teacher` (
     `tid` varchar(5) NOT NULL DEFAULT '',
     `tname` varchar(10) NOT NULL,
     PRIMARY KEY (`tid`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    CREATE TABLE `course` (
     `cid` varchar(5) NOT NULL DEFAULT '',
     `cname` varchar(10) NOT NULL,
     `tid` varchar(5) DEFAULT NULL,
     PRIMARY KEY (`cid`),
     KEY `fk_tid` (`tid`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


    CREATE TABLE `sc` (
     `sid` varchar(5) NOT NULL DEFAULT '',
     `cid` varchar(5) NOT NULL DEFAULT '',
     `score` float DEFAULT NULL,
     PRIMARY KEY (`sid`,`cid`),
     KEY `fk_cid` (`cid`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    INSERT  INTO  student VALUES ('01' , 'student A' , '1990-01-01', 'M');
    INSERT  INTO  student VALUES ('02' , 'student B' , '1990-12-21', 'M');
    INSERT  INTO  student VALUES ('03' , 'student C' , '1990-05-20', 'M');
    INSERT  INTO  student VALUES ('04' , 'student D' , '1990-08-06', 'M');
    INSERT  INTO  student VALUES ('05' , 'student E' , '1991-12-01', 'FM');
    INSERT  INTO  student VALUES ('06' , 'student F' , '1992-03-01', 'FM');
    INSERT  INTO  student VALUES ('07' , 'student G' , '1989-07-01', 'FM');
    INSERT  INTO  student VALUES ('08' , 'student H' , '1990-01-20', 'FM');

    INSERT  INTO  teacher VALUES ('01' , 'teacher A');
    INSERT  INTO  teacher VALUES ('02' , 'teacher B');
    INSERT  INTO  teacher VALUES ('03' , 'teacher C');

    INSERT  INTO  course VALUES ('01' , 'JAVA' , '01');
    INSERT  INTO  course VALUES ('02' , 'Python' , '02');
    INSERT  INTO  course VALUES ('03' , 'Mysql' , '03');
    INSERT  INTO  course VALUES ('04' , 'PHP' , '01');

    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 ('01' , '04' , 88);
    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);


select * from enter code here
student where sid in (
  select sid from sc 
  where cid in (select cid from sc where sid = '02')
  group by sid 
  having count(*) = (select count(*) from sc where sid = '02')
)
我用学生表,课程表和sc表(选择性课程)创建mysql数据库,我想查询sid =“02”学生学习同一课程的学生sid; 但上面的sql不对,“为什么?如何修复它?”

1 个答案:

答案 0 :(得分:0)

这就是你想要的吗?

SELECT DISTINCT s2.*
FROM student
JOIN sc ON sc.sid = student.sid
JOIN sc AS sc2 ON sc2.cid = sc.cid
JOIN student AS s2 ON s2.sid = sc2.sid
WHERE student.sid = '02'

http://sqlfiddle.com/#!9/1d6a1/4

另外一个建议是不要使用字符串作为键,当行数和比例变大时,它会使查询速度慢很多。