如何修改以下代码以获得与“存在”相同的结果?(SQL)
Posted
技术标签:
【中文标题】如何修改以下代码以获得与“存在”相同的结果?(SQL)【英文标题】:How can I modify the following code to have the same results with "exists"?(SQL) 【发布时间】:2010-12-14 23:01:29 【问题描述】:我已经尝试了几个小时,但我想不出任何东西,因为存在只返回布尔值,每次我尝试使用存在时,我要么取回整个结果列表,要么没有。
这是下面的代码,它返回我想要的结果。
SELECT DISTINCT Clients.Clients_Code
FROM Account
INNER JOIN Clients ON Account.Account_Number = Clients.Account_Number
INNER JOIN Credit_Card ON Account.Account_Number = Credit_Card.Account_Number
INNER JOIN Transactions ON Credit_Card.Credit_Number = Transactions.Credit_Number
INNER JOIN Transactions AS Transactions_1 ON Credit_Card.Credit_Number = Transactions_1.Credit_Number
INNER JOIN Transactions AS Transactions_2 ON Credit_Card.Credit_Number = Transactions_2.Credit_Number
INNER JOIN Transactions AS Transactions_3 ON Credit_Card.Credit_Number = Transactions_3.Credit_Number
WHERE (Transactions_3.Store_Code = '7182')
AND (Transactions_2.Store_Code = '2019')
AND (Transactions_1.Store_Code = '3121')
感谢大家的回复,尤其是本杰明。他的反应很完美
【问题讨论】:
如果您的查询格式更易读会更容易。我认为连接中有一个模式,但我看不到它。 是的,同意。太多的连接在这里进行。应该是一个视图。 你能解释一下你到底想达到什么目的吗,因为这样可以更容易地看到如何重写你的查询 【参考方案1】:根据我从您的详细信息中了解到的情况,您正试图找到一位在三个不同商店使用同一张信用卡的客户。以下查询将提供相同的结果。
SELECT Clients.Clients_Code
FROM Clients c
INNER JOIN Credit_Card cc ON cc.Account_Number = c.Account_Number
Where Exists(select * from Tranctions t with (nolock) where t.Store_Code = '7182' and t.Credit_Number = cc.Credit_Number)
AND Exists(select * from Tranctions t with (nolock) where t.Store_Code = '2019' and t.Credit_Number = cc.Credit_Number)
AND Exists(select * from Tranctions t with (nolock) where t.Store_Code = '3121' and t.Credit_Number = cc.Credit_Number)
另一种选择是:
SELECT Clients.Clients_Code
FROM Clients c
INNER JOIN Credit_Card cc ON cc.Account_Number = c.Account_Number
Where (select count(*) from Tranctions t with (nolock) where t.Store_Code in ('7182', '2019','3121') and t.Credit_Number = cc.Credit_Number) =3
【讨论】:
【参考方案2】:我怀疑你甚至需要 Account 表。
这行得通吗?
SELECT DISTINCT Clients.Clients_Code FROM Clients
INNER JOIN Credit_Card on Clients.Account_Number = Credit_Card.Account_Number
WHERE EXISTS (
SELECT null FROM Transactions
WHERE Transactions.Credit_Number = Credit_Card.Credit_Number
AND Transactions.Store_Code in ('7182','2019','3121')
)
【讨论】:
这将返回比原始查询更多的结果,因为它将返回在三个商店中的任何一个使用信用卡的任何客户的客户代码。原始查询选择在所有三个商店都使用同一张卡的客户。 你是对的 - 有点过分热衷于简化的心情错过了! 是的,我第一次看时也做了同样的事情。【参考方案3】:我不确定我是否完全理解您的问题。您是否在问该查询如何返回布尔值而不是实际记录?如果是这样:
IF EXISTS (SELECT DISTINCT Clients.Clients_Code
FROM Account
INNER JOIN Clients
ON Account.Account_Number = Clients.Account_Number
INNER JOIN Credit_Card
ON Account.Account_Number = Credit_Card.Account_Number
INNER JOIN Transactions
ON Credit_Card.Credit_Number = Transactions.Credit_Number
INNER JOIN Transactions AS Transactions_1
ON Credit_Card.Credit_Number = Transactions_1.Credit_Number
INNER JOIN Transactions AS Transactions_2
ON Credit_Card.Credit_Number = Transactions_2.Credit_Number
INNER JOIN Transactions AS Transactions_3
ON Credit_Card.Credit_Number = Transactions_3.Credit_Number
WHERE (Transactions_3.Store_Code = '7182')
AND (Transactions_2.Store_Code = '2019')
AND (Transactions_1.Store_Code = '3121'))
SELECT 1 --True
ELSE
SELECT 0 --False
如果这不是您想要完成的任务,您能否澄清您的问题?
【讨论】:
【参考方案4】:你的查询很奇怪。
我认为您可以使用此查询获得相同的结果:
SELECT DISTINCT c.Clients_Code
FROM Account AS a
INNER JOIN Clients AS c
ON a.Account_Number = c.Account_Number
INNER JOIN Credit_Card AS cc
ON a.Account_Number = cc.Account_Number
INNER JOIN Transactions AS t
ON cc.Credit_Number = t0.Credit_Number
WHERE t.Store_Code IN ('7182', '2019', '3121')
如果您想知道该查询是否存在任何结果,那么您只想将整个内容包装在 EXISTS([above_query]) 中。
【讨论】:
以上是关于如何修改以下代码以获得与“存在”相同的结果?(SQL)的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 fetch() 获得与 fetchAll() 相同的结果?
如何使用相同的测试数据测试 .tflite 模型以证明它的行为与原始模型相同?