2017-03-07 55 views
1

我有这些表和值:查询,而无需使用除了

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”但不是在体裁“爵士”的仪器。我需要做到这一点,而无需使用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' 
       ) 

我无法弄清楚。感谢帮助!

+0

你也许是学生吗?我的笔记本电脑上有完全相同的桌子和东西供学校使用。 – iPhantomGuy

+0

是的,我准备明天 – Yakalent

+0

不错,我也是韩国人。大测试来了吧? – iPhantomGuy

回答

2

我会用group byhaving。这里有一个方法:

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有名字。

+0

Thanx的答案,我不明白的是在这种情况下使用最大和最小值你能解释这一点吗? – Yakalent

+0

@Yakalent。 。 。它只是检查价值总是“klassiek”而不是“爵士乐”。还有其他方法,但这种方式非常简单。 –

+0

感谢您的解释! – Yakalent

1

由戈登·利诺夫答案(+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()

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' 
) 

rextester演示:http://rextester.com/YGJEJ76277