首先,我想为我糟糕的英语道歉。 我在 mySQL 上遇到问题,但没有找到正确的解决方案。
我有 3 个表:
CREATE TABLE branch (name CHAR(30), city VARCHAR(30), assets NUMERIC(16,2), PRIMARY KEY (name));
CREATE TABLE account (accountNr CHAR(9), branch CHAR(30), balance NUMERIC(12,2), PRIMARY KEY (accountNr), FOREIGN KEY (branch) REFERENCES branch(name));
CREATE TABLE customer (customerId INT(5), accountNr CHAR(9), PRIMARY KEY (customerId, accountNr), FOREIGN KEY (accountNr) REFERENCES account(accountNr));
我应该找到一个请求,以查找在 city = 'New York' 的所有分行中拥有帐户的客户的 customerId。我必须使用“不存在”条件。
所以我知道,我有两个金额:
A.客户拥有账户的分支机构数量。
B.纽约所有分支机构的数量
想法:选择 B 是 A 子集的所有客户
最佳答案
您可以尝试以下查询:
select distinct customerId from customer out_customer where ( select count(account.accountNr) from account inner join branch on account.branch = branch.name INNER join customer ON customer.accountNr = account.accountNr where city='New York' AND out_customer.customerId = customer.customerId ) = (select count(name) from branch where city='New York')
关于MySQL:使用 NOT EXISTS 搜索请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34043876/