0

私の質問は次のとおりです: (部屋 115 にある、または 5 人以上の学生が在籍しているすべてのクラスの名前を見つけてください。)

CREATE TABLE STUDENT (
    sid NUMERIC(6) NOT NULL,
    sname VARCHAR(15),
    major VARCHAR(20),
    level VARCHAR(2),
    sage NUMERIC(2) NOT NULL,
    PRIMARY KEY (sid)
    );
CREATE TABLE CLASS(
    cnum VARCHAR(6),
    meets_at TIME,
    room VARCHAR(6),
    fid NUMERIC(6) ,
    PRIMARY KEY (cnum),
    FOREIGN KEY (fid)
    references FACULTY (fid)     
   );
CREATE TABLE ENROLLED (   
    cnum VARCHAR(6),
    sid NUMERIC(6) NOT NULL,
    PRIMARY KEY (cnum,sid),
  FOREIGN KEY (sid)
       REFERENCES STUDENT (sid),
        FOREIGN KEY (sid)
       REFERENCES FACULTY (fid),
       FOREIGN KEY (cnum)
       REFERENCES CLASS (cnum)        
    );
  alter table ENROLLED 
   add foreign key (sid)
   references STUDENT (sid);
4

2 に答える 2

1

多分このようなもの:

SELECT
    *
FROM
    CLASS
WHERE
    CLASS.room='115'
    OR
    (
        SELECT
            COUNT(*)
        FROM
            ENROLLED
            JOIN STUDENT
                ON ENROLLED.sid=STUDENT.sid
        WHERE
            ENROLLED.cnum=CLASS.cnum
    )>=5
于 2012-04-17T07:17:11.490 に答える
0
SELECT `sid`, `cid` 
FROM (`STUDENT` INNER JOIN `ENROLLED` ON `STUDENT`.`sid` = `ENROLLED`.`sid`) 
    INNER JOIN `CLASS` ON `ENROLLED`.`cnum` = `CLASS`.`cnum` 
GROUP BY `ENROLLED`.`cid` 
HAVING 
    `CLASS`.`room` = '115' OR 
    COUNT(DISTINCT `STUDENT`.`sid`) >= 5
于 2012-04-17T07:18:11.077 に答える