需要帮助我使用什么语法来找出没有专业的大学几何课程的部门。
以下是原始问题: 对于那些没有专业学习大学几何课程的部门, 打印部门名称和部门博士生人数。
我已经得出结果显示部门中的所有部门和博士生,但无法弄清楚用什么语法来找出没有专业的大学几何课程的部门。我使用了EXCEPT,NOT IN和LEFT JOIN,但我没有得到我正在寻找的东西。任何建议..
以下是我的表格:
Student(sid,sname,sex,age,year,qpa)
Dept(dname,numphds)
Prof (pname,dname)
Course (cno,cname,dname)
Major(dname,sid)
Section(dname,cno,sectno,pname)
Enroll(sid,grade,dname,cno,sectno)
这是我的dept表的样子:
CREATE TABLE dept (
dname VARCHAR(100),
numphds INT
);
INSERT INTO dept VALUES ( 'Chemical Engineering', 32 );
INSERT INTO dept VALUES ( 'Civil Engineering', 88 );
INSERT INTO dept VALUES ( 'Computer Sciences', 47 );
INSERT INTO dept VALUES ( 'Industrial Engineering', 41 );
INSERT INTO dept VALUES ( 'Mathematics', 129 );
INSERT INTO dept VALUES ( 'Sanitary Engineering', 3 );
这是我的课程表的样子,它是唯一一张显示“College Geometry”的表格。
CREATE TABLE course (
cno INT,
cname VARCHAR(100),
dname VARCHAR(100)
);
INSERT INTO course VALUES ( 302, 'Intro to Programming', 'Computer Sciences' );
INSERT INTO course VALUES ( 310, 'Thermodynamics', 'Chemical Engineering' );
INSERT INTO course VALUES ( 310, 'Intro to Garbage', 'Sanitary Engineering' );
INSERT INTO course VALUES ( 365, 'City Planning', 'Civil Engineering' );
INSERT INTO course VALUES ( 375, 'Highway Engineering', 'Civil Engineering' );
INSERT INTO course VALUES ( 461, 'College Geometry 1', 'Mathematics' );
INSERT INTO course VALUES ( 462, 'College Geometry 2', 'Mathematics' );
这是我的代码:
SELECT dept.dname, dept.numphds
FROM dept
LEFT JOIN course on course.dname = dept.dname LIKE 'College Geometry'
GROUP BY dept.dname, dept.numphds
答案 0 :(得分:1)
添加where
子句并检查course
列
SELECT dept.dname, dept.numphds
FROM dept
LEFT JOIN course on course.dname = dept.dname
and course.cname LIKE '%College Geometry%'
where course.dname is NULL
您可以使用not exists
子句
SELECT dept.dname, dept.numphds
FROM dept
WHERE NOT EXISTS
( select 1 from course
where course.dname = dept.dname
and course.cname LIKE '%College Geometry%'
)