如何在 sql 中挑出某些名字

how to single out certain names in sql

所以我在 SQL 中遇到了这个问题,问题是这样的:List the sId and name of students that applied to “WSU” But not “U of O”. 而我的尝试是这样的:

SELECT DISTINCT sName, 
       student.sID 
FROM Student 
LEFT JOIN Apply ON Student.sID = Apply.sID 
WHERE (cName<>'U of O' and cName = 'WSU') 
ORDER BY sName ASC;

然而,这并没有起到作用,因为它没有发现一些学生已经申请了俄勒冈大学。 MRE:

CREATE TABLE IF NOT EXISTS College
(
  State CHAR(2) NOT NULL,
  cName VARCHAR(20) NOT NULL,
  enrollment INT NOT NULL,
  PRIMARY KEY (cName)
);

CREATE TABLE IF NOT EXISTS Major
(
  major VARCHAR(30) NOT NULL,
  PRIMARY KEY (major)
);

CREATE TABLE IF NOT EXISTS Student
(
  sID INT NOT NULL,
  sName VARCHAR(30) NOT NULL,
  GPA FLOAT NOT NULL,
  sizeHS INT NOT NULL,
  PRIMARY KEY (sID)
);

CREATE TABLE IF NOT EXISTS MinimumGPA
(
  minGPA FLOAT NOT NULL,
  cName VARCHAR(20) NOT NULL,
  major VARCHAR(30) NOT NULL,
  PRIMARY KEY (cName, major),
  FOREIGN KEY (cName) REFERENCES College(cName),
  FOREIGN KEY (major) REFERENCES Major(major)
);

CREATE TABLE IF NOT EXISTS APPLY
(
  decision SET('Y', 'N') NOT NULL,
  sID INT NOT NULL,
  cName VARCHAR(20) NOT NULL,
  major VARCHAR(30) NOT NULL,
  PRIMARY KEY (sID, cName, major),
  FOREIGN KEY (sID) REFERENCES Student(sID),
  FOREIGN KEY (cName, major) REFERENCES MinimumGPA(cName, major)
);

INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (123, 'Amy', 3.90, 1000);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (234, 'Bob', 3.60, 1500);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (345, 'Craig', 3.50, 500);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (456, 'Doris', 3.90, 1000);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (543, 'Craig', 3.40, 2000);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (567, 'Edward', 2.90, 2000);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (654, 'Amy', 3.90, 1000);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (678, 'Fay', 3.80, 200);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (765, 'Jay', 2.90, 1500);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (789, 'Gary', 3.40, 800);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (876, 'Irene', 3.90, 400);
INSERT INTO Student (sId, sName, GPA, sizeHS) VALUES (987, 'Helen', 4.00, 800);

INSERT INTO College (cName, State, enrollment) VALUES ('Cornell', 'NY', 21000);
INSERT INTO College (cName, State, enrollment) VALUES ('MIT', 'MA', 10000);
INSERT INTO College (cName, State, enrollment) VALUES ('WSU', 'WA', 28000);
INSERT INTO College (cName, State, enrollment) VALUES ('U of O', 'OR', 25000);

INSERT INTO Major (major) VALUES ('CS');
INSERT INTO Major (major) VALUES ('EE');
INSERT INTO Major (major) VALUES ('history');
INSERT INTO Major (major) VALUES ('biology');
INSERT INTO Major (major) VALUES ('bioengineering');
INSERT INTO Major (major) VALUES ('psychology');
INSERT INTO Major (major) VALUES ('marine biology');

INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('WSU', 'CS', 3.75);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('WSU', 'EE', 3.5);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('WSU', 'history', 2.8);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('U of O', 'CS', 3.6);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('U of O', 'biology', 3.75);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('Cornell','bioengineering', 3.8);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('Cornell','CS', 3.4);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('Cornell','EE', 3.6);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('Cornell','history', 3.6);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('Cornell','psychology', 2.8);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('MIT', 'biology', 3.5);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('MIT', 'bioengineering', 3.5);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('MIT', 'CS', 3.9);
INSERT INTO MinimumGPA (cName, major, minGPA) VALUES ('MIT', 'marine biology', 3.5);

INSERT INTO Apply (sID, cName, major, decision) VALUES (123, 'Cornell', 'EE', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (123, 'WSU', 'CS', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (123, 'WSU', 'EE', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (123, 'U of O', 'CS', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (123, 'MIT', 'CS', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (234, 'U of O', 'biology', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (345, 'Cornell', 'bioengineering', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (345, 'Cornell', 'CS', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (345, 'Cornell', 'EE', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (345, 'MIT', 'bioengineering', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (543, 'MIT', 'CS', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (678, 'Cornell', 'history', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (678, 'Cornell', 'psychology', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (678, 'WSU', 'history', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (765, 'WSU', 'history', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (876, 'MIT', 'biology', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (876, 'MIT', 'marine biology', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (876, 'WSU', 'CS', 'N');
INSERT INTO Apply (sID, cName, major, decision) VALUES (987, 'WSU', 'CS', 'Y');
INSERT INTO Apply (sID, cName, major, decision) VALUES (987, 'U of O', 'CS', 'Y');

CREATE TABLE IF NOT EXISTS CollegeStats
(
  cName VARCHAR(20) NOT NULL,
  appCount INT NOT NULL,
  minGPA dec(3, 2) NOT NULL,
  maxGPA dec(3, 2) NOT NULL,
  PRIMARY KEY (cName)
);

当然还有最后我要处理的功能:

SELECT DISTINCT sName, 
       student.sID 
FROM Student 
LEFT JOIN Apply ON Student.sID = Apply.sID 
WHERE (cName<>'U of O' and cName = 'WSU') 
ORDER BY sName ASC;

我得到了什么:

'Amy', '123'
'Fay', '678'
'Helen', '987'
'Irene', '876'
'Jay', '765'

预计:

'Fay', '678'
'Irene', '876'
'Jay', '765'


SELECT * FROM Student RIGHT JOIN Apply ON Student.sID = Apply.sID ORDER BY sName ASC;
SELECT DISTINCT sName, student.sID FROM Student LEFT JOIN Apply ON Student.sID = Apply.sID WHERE (cName<>'U of O' and cName = 'WSU') ORDER BY sName ASC;

上面我想要完成的关系应该很好地列出了学生申请的所有学校和专业。

您不需要加入。你想见学生?所以,select来自学生table。他们应符合标准?使用 where 子句。 Straight-forward 带有 IN 个子句:

SELECT *
FROM Student 
WHERE sid IN (SELECT sid FROM apply WHERE cname = 'WSU')
  AND sid NOT IN (SELECT sid FROM apply WHERE cname = 'U of O')
ORDER BY sName ASC;

您可以使用 EXISTS 而不是 EXISTS,后者对于更大的数据可能执行得更快。与@Thorsten Kettner answer 相同,仅替换为 exists.

SELECT sID,sName
FROM Student 
WHERE EXISTS   ( SELECT sid 
                 FROM Apply 
                 WHERE Student.sID = Apply.sID 
                 AND  cname = 'WSU'
                 )
AND NOT EXISTS   ( SELECT sid 
                   FROM Apply 
                   WHERE Student.sID = Apply.sID  
                   AND cname = 'U of O'
                  )
ORDER BY sName ASC;

Result:

sID   sName
678   Fay
876   Irene
765   Jay

Demo

您可以使用聚合并在 HAVING 子句中设置条件:

SELECT s.sID, s.sName
FROM Student s INNER JOIN Apply a
ON a.sID = s.sID 
WHERE a.cName IN ('WSU', 'U of O') 
GROUP BY s.sID
HAVING COUNT(CASE WHEN a.cName = 'U of O' THEN 1 END) = 0
ORDER BY s.sName ASC;

或:

SELECT s.sID, s.sName
FROM Student s INNER JOIN Apply a
ON a.sID = s.sID 
GROUP BY s.sID
HAVING COUNT(CASE WHEN a.cName = 'WSU' THEN 1 END) > 0
   AND COUNT(CASE WHEN a.cName = 'U of O' THEN 1 END) = 0
ORDER BY s.sName ASC;

参见demo