简体   繁体   English

SQL - 根据另一个表中指定的条件限制一个表的结果

[英]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.) (我仍然是一个SQL新手。)

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. 即告诉我所有不持有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. SQL作为描述性语言的好处在于它非常好地从英语翻译。 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: 转换为SQL,这将是[not] exists运算符:

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语句。 Not in asks SQL to specifically exclude those product types which match the criteria in where condition in sub query Not in要求SQL专门排除那些符合sub querywhere条件的条件的product types

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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