简体   繁体   中英

SQL - Limiting results from one table, based on criteria specified in another

This has been asked many times before, but I'm having trouble understanding previous solutions and implementing them into my own query. (I am very much still a SQL novice.)

I have the following query:

select *
from **Product** prod
inner JOIN
**account** acct on prod.product_id = acct.product_id
inner JOIN
**client_account_relationship** car on acct.account_id = car.ACCOUNT_ID
inner JOIN
**client** cl on car.client_id = cl.client_id
where prod.product_code != 'Producttype'

My table structure is as follows:

Product - This table contains what products a client account holds

Account - This table holds what accounts a client holds

Client Account Relationship - This table holds the link between clients and accounts

Client - This table holds clients

A Product will always have an Account, an Account will always have a Client/Account Relationship, and a Client/Account Relationship will always have a Client.

I want to show display all clients that do NOT hold a specific product type. Ie Show me all Clients that do not hold ProductType1. But because a Client can hold many different product types, my query will show me all the products except the one I'm excluding, but a client might still hold that excluded product.

How do I limit results from the Client Table based on criteria set in another table?

The nice thing about SQL being a descriptive language is that it translates from English quite nicely. You already described what you want to do - find all the clients that don't have a specific product. Translated to SQL, this would be the [not] exists operator:

SELECT *
FROM   client c
WHERE  NOT EXISTS (SELECT *
                   FROM   product p
                   JOIN   account a ON p.product_id = a.product_id
                   JOIN   client_account_relationship car ON 
                          a.account_id = car.ACCOUNT_ID
                   WHERE  car.client_id = c.client_id AND
                          p.product_code = 'Producttype')

Using NOT IN statement. Not in asks SQL to specifically exclude those product types which match the criteria in where condition in sub query

select distinct car.client_id
from **Product** prod
inner JOIN
**account** acct on prod.product_id = acct.product_id
inner JOIN
**client_account_relationship** car on acct.account_id = car.ACCOUNT_ID
inner JOIN
**client** cl on car.client_id = cl.client_id
where car.client_id not in 
(select car.client_id 
from **Product** prod
inner JOIN
**account** acct on prod.product_id = acct.product_id
inner JOIN
**client_account_relationship** car on acct.account_id = car.ACCOUNT_ID
inner JOIN
**client** cl on car.client_id = cl.client_id
where prod.product_code != 'ProductType1'
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM