Sql 不使用except的查询
我有这些表和值:Sql 不使用except的查询,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有这些表和值: create table Instrument ( instrumentnaam varchar(14) not null, toonhoogte varchar(7) not null ) INSERT INTO instrument VALUES ('piano', '' ); INSERT INTO instrument VALUES ('fluit', ''
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”类型中使用的乐器。我需要这样做,不使用除了
这是我的尝试:
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'
)
我想不出来。谢谢你的帮助 我会使用
分组依据
和拥有
。这里有一种方法:
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';
注意:您不需要
仪器
,因为您在bezettingsregel
中有名称相比之下,Gordon Linoff(+1)的答案要简单得多,但下面是如何使用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演示:Thanx对于答案,我不明白的是在这种情况下使用max和min,你能解释一下吗?@Yakalent。它只是检查值是否总是
'klassiek'
,而不是'jazz'
。还有其他方法,但这种方法很简单。谢谢你的解释!你可能是学生吗?我的笔记本电脑上有完全一样的桌子和东西要上学。是的,我在为明天的冰做准备,我也是汉族人。大考就要来了,哈?哈哈,没错
select distinct b.instrumentnaam
from bezettingsregel b
inner join stuk s
on b.stuknr = s.stuknr
where s.genrenaam = 'klassiek'
and b.instrumentnaam not in (
select instrumentnaam
from stuk nes
inner join bezettingsregel neb
on neb.stuknr = nes.stuknr
where nes.genrenaam = 'jazz'
)