我不知道SQL SERVER中的除法是如何工作的(使用NOT EXISTS...(NOT EXISTS))

问题描述 投票:0回答:1

我有3张桌子

PRODUCT          
    ID  -------COUNTRY    
    1   --------Singapore  
    2   --------VietNam   
    3   --------Singapor   
    4   --------Campuchia
RECEIPT
IDPRO -------IDBILL
    1 -------A
    2 -------B
    3 -------A
    3 -------C
    4 -------B
BILL 
   CODE 
    A
    B
    C

老师问的问题:列出所有代码 所有产品都是从新加坡买的

这是他的回答:

SELECT CODE
FROM BILL B
WHERE NOT EXISTS (
SELECT * FROM PRODUCT P
WHERE COUNTRY='Singagpore' and not exists(
SELECT * FROM RECEIPT R
WHERE B.CODE=R.IDBILL AND P.ID=R.IDPRO))

效果很好 结果是:A

问题是当我运行子查询的命令行时,没有结果

SELECT * FROM PRODUCT P
    WHERE COUNTRY='Singagpore' and not exists(
    SELECT * FROM RECEIPT R
    WHERE  P.ID=R.IDPRO)

假设上面的结果被赋值为K。

命令行的其余部分将是:

SELECT CODE
FROM BILL B
WHERE NOT EXISTS (K)

它用空结果作为参考,怎么能得到像我老师那样的结果?

我已经搜索过如何使用

NOT EXISTS
,但是除法有点困难

sql relational-division not-exists
1个回答
0
投票

如果您向我们提供这样的表结构和数据,那将会非常有帮助:

 CREATE TABLE [dbo].[bill] (
        [code]  VARCHAR(100)    NOT NULL    
    );
    
    CREATE TABLE [dbo].[product] (
        [product_id]    INT         NOT NULL,
        [country]   VARCHAR(100)    NOT NULL
    );
    
    CREATE TABLE [dbo].[receipt] (
        [receipt_id]    INT         NOT NULL IDENTITY (1, 1),
        [product_id]    INT         NOT NULL,
        [bill_id]   VARCHAR(100)    NOT NULL
    );
    --insert data in tables:
    INSERT INTO bill (code) values ('A')
    INSERT INTO bill (code) values ('B')
    INSERT INTO bill (code) values ('C')        
    INSERT INTO product (product_id, country) values (1,'Singapore')
    INSERT INTO product (product_id, country) values (2,'VietNam')
    INSERT INTO product (product_id, country) values (3,'Singapore')
    INSERT INTO product (product_id, country) values (4,'Campuchia')      
    INSERT INTO receipt (product_id, bill_id) values (1,'A')
    INSERT INTO receipt (product_id, bill_id) values (2,'B')
    INSERT INTO receipt (product_id, bill_id) values (3,'A')
    INSERT INTO receipt (product_id, bill_id) values (3,'C')
    INSERT INTO receipt (product_id, bill_id) values (4,'B')
    
    --This returns A:        
    SELECT code
    FROM BILL B
    WHERE NOT EXISTS (
    SELECT * FROM PRODUCT P
    WHERE COUNTRY='Singapore' and not exists(
    SELECT * FROM RECEIPT R
    WHERE B.CODE=R.bill_id AND P.product_id=R.product_id))

这是因为 NOT EXISTS 子句中的代码将对 BILL 中的每一行运行,如果选择不返回任何内容,则该行将被选择。如果选择返回一些内容,那么它将不会被选择。如果您使用 EXISTS 而不是 NOT EXISTS,则会发生相反的情况:

    --returns nothing for A, so A will be returned
    SELECT * FROM PRODUCT P
    WHERE COUNTRY='Singapore' and not exists(
    SELECT * FROM RECEIPT R
    WHERE 'A'=R.bill_id AND P.product_id=R.product_id)
    --returns something for B, so B will not be returned
    SELECT * FROM PRODUCT P
    WHERE COUNTRY='Singapore' and not exists(
    SELECT * FROM RECEIPT R
    WHERE 'B'=R.bill_id AND P.product_id=R.product_id)
    ----returns something for C, so C will not be returned
    SELECT * FROM PRODUCT P
    WHERE COUNTRY='Singapore' and not exists(
    SELECT * FROM RECEIPT R
    WHERE 'C'=R.bill_id AND P.product_id=R.product_id)
       
© www.soinside.com 2019 - 2024. All rights reserved.