我有这些表和值:
create table Instrument (
instrumentnaam varchar(14) not null,
toonhoogte varchar(7) not null
)
INSERT INTO instrument VALUES ('piano', '' );
INSERT INTO instrument VALUES ('fluit', '' );
INSERT INTO instrument VALUES ('fluit', 'alt' );
INSERT INTO instrument VALUES ('saxofoon', 'alt' );
INSERT INTO instrument VALUES ('saxofoon', 'tenor' );
INSERT INTO instrument VALUES ('saxofoon', 'sopraan');
INSERT INTO instrument VALUES ('gitaar', '' );
INSERT INTO instrument VALUES ('viool', '' );
INSERT INTO instrument VALUES ('viool', 'alt' );
INSERT INTO instrument VALUES ('drums', '' );
create table Bezettingsregel (
stuknr numeric(5) not null,
instrumentnaam varchar(14) not null,
toonhoogte varchar(7) not null,
aantal numeric(2) not null
)
INSERT INTO bezettingsregel VALUES ( 2, 'drums', '', 1);
INSERT INTO bezettingsregel VALUES ( 2, 'saxofoon', 'alt', 2);
INSERT INTO bezettingsregel VALUES ( 2, 'saxofoon', 'tenor', 1);
INSERT INTO bezettingsregel VALUES ( 2, 'piano', '', 1);
INSERT INTO bezettingsregel VALUES ( 3, 'fluit', '', 1);
INSERT INTO bezettingsregel VALUES ( 5, 'fluit', '', 3);
INSERT INTO bezettingsregel VALUES ( 9, 'fluit', '', 1);
INSERT INTO bezettingsregel VALUES ( 9, 'fluit', 'alt', 1);
INSERT INTO bezettingsregel VALUES ( 9, 'piano', '', 1);
INSERT INTO bezettingsregel VALUES (12, 'piano', '', 1);
INSERT INTO bezettingsregel VALUES (12, 'fluit', '', 2);
INSERT INTO bezettingsregel VALUES (13, 'drums', '', 1);
INSERT INTO bezettingsregel VALUES (13, 'saxofoon', 'alt', 1);
INSERT INTO bezettingsregel VALUES (13, 'saxofoon', 'tenor', 1);
INSERT INTO bezettingsregel VALUES (13, 'fluit', '', 2);
INSERT INTO bezettingsregel VALUES (14, 'piano', '', 1);
INSERT INTO bezettingsregel VALUES (14, 'fluit', '', 1);
INSERT INTO bezettingsregel VALUES (15, 'saxofoon', 'alt', 2);
INSERT INTO bezettingsregel VALUES (15, 'fluit', 'alt', 2);
INSERT INTO bezettingsregel VALUES (15, 'piano', '', 1);
create table Stuk (
stuknr numeric(5) not null,
componistId numeric(4) not null,
titel varchar(20) not null,
stuknrOrigineel numeric(5) null,
genrenaam varchar(10) not null,
niveaucode char(1) null,
speelduur numeric(3,1) null,
jaartal numeric(4) not null
)
INSERT INTO stuk VALUES ( 1, 1, 'Blue bird', NULL, 'jazz', NULL, 4.5, 1954);
INSERT INTO stuk VALUES ( 2, 2, 'Blue bird', 1, 'jazz', 'B', 4, 1988);
INSERT INTO stuk VALUES ( 3, 4, 'Air pur charmer', NULL, 'klassiek', 'B', 4.5, 1953);
INSERT INTO stuk VALUES ( 5, 5, 'Lina', NULL, 'klassiek', 'B', 5, 1979);
INSERT INTO stuk VALUES ( 8, 8, 'Berceuse', NULL, 'klassiek', NULL, 4, 1786);
INSERT INTO stuk VALUES ( 9, 2, 'Cradle song', 8, 'klassiek', 'B', 3.5, 1990);
INSERT INTO stuk VALUES (10, 8, 'Non piu andrai', NULL, 'klassiek', NULL, NULL, 1791);
INSERT INTO stuk VALUES (12, 9, 'I''ll never go', 10, 'pop', 'A', 6, 1996);
INSERT INTO stuk VALUES (13, 10, 'Swinging Lina', 5, 'jazz', 'B', 8, 1997);
INSERT INTO stuk VALUES (14, 5, 'Little Lina', 5, 'klassiek', 'A', 4.3, 1998);
INSERT INTO stuk VALUES (15, 10, 'Blue sky', 1, 'jazz', 'A', 4, 1998);
现在我想写一个查询,显示在'klassiek'类型中使用的乐器,但不是在'jazz'类型中。我需要在不使用EXCEPT
的情况下执行此操作这是我的尝试:
SELECT i.instrumentnaam
FROM instrument i inner join
bezettingsregel b on i.instrumentnaam = b.instrumentnaam
inner join stuk s on b.stuknr = s.stuknr
WHERE genrenaam = 'klassiek'
AND NOT EXISTS (SELECT stuknr
FROM stuk
WHERE genrenaam = 'jazz'
)
我无法理解。 Thanx寻求帮助!
答案 0 :(得分:2)
我会使用group by
和having
。这是一种方法:
SELECT b.instrumentnaam
FROM bezettingsregel b inner join
stuk s
on b.stuknr = s.stuknr
WHERE s.genrenaam IN ('klassiek', 'jazz')
GROUP BY b.instrumentnaam
HAVING MAX(s.genrenaam) = 'klassiek' AND MIN(s.genrenaam) = 'klassiek';
注意:您不需要Instrument
,因为您的名字位于bezettingsregel
。
答案 1 :(得分:1)
相比之下,Gordon Linoff(+1)的答案要简单得多,但这里是你如何使用false
做你想做的事情:
not exists()
以下是select distinct b.instrumentnaam
from bezettingsregel b
inner join stuk s
on b.stuknr = s.stuknr
where s.genrenaam = 'klassiek'
and not exists (
select 1
from stuk nes
inner join bezettingsregel neb
on neb.stuknr = nes.stuknr
where nes.genrenaam = 'jazz'
and neb.instrumentnaam = b.instrumentnaam
)
instrumentnaam not in()
rextester演示:http://rextester.com/YGJEJ76277