我已经尝试了几个小时,但我想不出任何东西,因为存在只返回布尔值,每次我尝试使用存在时,我要么取回整个结果列表,要么没有。
以下代码返回了我想要的结果。
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')
感谢您的回应和特别是BENJAMIN。他的回应完美无缺
答案 0 :(得分:2)
根据我从您的详细信息中得知,您正在尝试找到在三个不同商店使用相同信用卡的客户。以下查询将提供相同的结果。
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
答案 1 :(得分:1)
我怀疑你甚至需要账户表。
这有用吗?
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')
)
答案 2 :(得分:0)
我不确定我完全理解你的问题。您是否询问该查询如何返回布尔值而不是实际记录?如果是这样的话:
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
如果那不是你想要完成的,请你澄清一下你的问题吗?
答案 3 :(得分:0)
您的查询是......奇怪。
我认为您可以使用此查询获得相同的结果:
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])中。