我只想选择没有产品ID为11的行的学生ID。但我不能。我该怎么办?
BEGIN TRANSACTION;
CREATE TABLE STUDENTS(Id integer PRIMARY KEY, name text,year_born integer
);
CREATE TABLE PROJECT(project_id integer,title text,project_owner
text,year_written integer );
CREATE TABLE PROJECTWORKS(student_id integer,project_id integer);
INSERT INTO STUDENTS VALUES(1598,'james',1996);
INSERT INTO STUDENTS VALUES(2479,'andre',1996);
INSERT INTO STUDENTS VALUES(3682,'pierre',1997);
INSERT INTO PROJECT VALUES(10,'A','ABC',2008);
INSERT INTO PROJECT VALUES(11,'B','ABC',2010);
INSERT INTO PROJECT VALUES(12,'C','ABC',2016);
INSERT INTO PROJECT VALUES(13,'D','CBA',2014);
INSERT INTO PROJECTWORKS VALUES(1598,10);
INSERT INTO PROJECTWORKS VALUES(1598,11);
INSERT INTO PROJECTWORKS VALUES(1598,12);
INSERT INTO PROJECTWORKS VALUES(3682,12);
INSERT INTO PROJECTWORKS VALUES(3682,13);
INSERT INTO PROJECTWORKS VALUES(2479,12);
SELECT * FROM STUDENTS;
SELECT * FROM PROJECT;
SELECT * FROM PROJECTWORKS;
SELECT DISTINCT student_id FROM PROJECTWORKS
WHERE not project_id=11 and (project_id=10 OR project_id=12 OR
project_id=13);
我只想 3682 和 2479 因为 1598 有11个。
答案 0 :(得分:1)
NOT EXISTS非常适合:
SELECT DISTINCT student_id
FROM PROJECTWORKS p
WHERE project_id IN (10, 12, 13)
AND NOT EXISTS
(SELECT 1
FROM PROJECTWORKS p2
WHERE p.student_id = p2.student_id
AND p2.project_id = 11);
此外,我认为你的意思是你不想要ID 11,因为我在你的例子中没有看到14。
答案 1 :(得分:0)
如果我理解你,这就是你想要的
[System.Byte[]]::CreateInstance(<Length>)
答案 2 :(得分:0)
您可以在两个结果之间使用连接
select distinct student_id
from PROJECTWORKS p
where project_id NOT IN (11)
INNER JOIN (
Select distinct student_id
from PROJECTWORKS
where project_id IN ( 10, 12, 13)
) T on t.student_id = p.student_id
答案 3 :(得分:0)
SELECT DISTINCT student_id FROM PROJECTWORKS
WHERE student_id not in
(select student_id from PROJECTWORKS where project_id=11)
你的问题不明确。上面的代码片段用于不同的student_id,其中没有project_id为11