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

Mysql SQL-根据另一个表中指定的条件限制一个表的结果,mysql,sql,database,join,Mysql,Sql,Database,Join,这已经被问过很多次了,但是我很难理解以前的解决方案并将它们实现到我自己的查询中。(我还是个SQL新手。) 我有以下疑问: 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

这已经被问过很多次了,但是我很难理解以前的解决方案并将它们实现到我自己的查询中。(我还是个SQL新手。)

我有以下疑问:

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'
我的表格结构如下:

产品-此表包含客户帐户持有的产品

Account(帐户)-此表包含客户端拥有的帐户

客户-帐户关系-此表保存客户和帐户之间的链接

客户机-此表包含客户机

产品将始终有一个帐户,帐户将始终有一个客户/帐户关系,客户/帐户关系将始终有一个客户

我想显示不包含特定产品类型的所有客户端。即,向我展示所有不持有ProductType1的客户。但由于客户可以持有许多不同的产品类型,因此我的查询将显示除我排除的产品之外的所有产品,但客户可能仍然持有排除的产品


如何根据另一个表中设置的条件限制客户端表中的结果

SQL作为一种描述性语言的好处在于它可以很好地从英语翻译过来。你已经描述了你想要做什么-找到所有没有特定产品的客户。转换为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')

SQL作为一种描述性语言的好处在于它可以很好地从英语翻译过来。你已经描述了你想要做什么-找到所有没有特定产品的客户。转换为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')

在语句中不使用
<代码>不在
中要求SQL明确排除那些与子查询中的条件相匹配的产品类型

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

在语句中不使用
<代码>不在
中要求SQL明确排除那些与子查询中的条件相匹配的产品类型

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

您肯定缺少了几个连接,是否映射了查询中的所有FK?您肯定缺少了几个连接,是否映射了查询中的所有FK?两种解决方案都工作得很好,但第二种解决方案对我更有用,因为它保持了连接数据的完整性,这是其他字段信息所需要的。我想我现在开始明白如何使用不存在和不存在。它们似乎都做了相同的事情,但是有没有一个原因是其中一个比另一个更适合使用?我在大多数情况下都不使用,因为我直接引用了我在Not In语句中要求的列,根据我的经验,该列比不存在的列工作得更快,该列处理所有列。两种解决方案都工作得很好,但是第二种方法对我来说更有用,因为它保持了连接数据的完整性,这是额外字段信息所需要的。我想我现在开始明白如何使用不存在和不存在。它们似乎都做了相同的事情,但有没有一个原因是其中一个比另一个更可取?我在大多数情况下都不使用,因为我直接引用了我在Not In语句中要求的那个列,根据我的经验,它比不存在的要快,它处理所有列