MySQL:使用 NOT EXISTS 搜索请求

标签 mysql

首先,我想为我糟糕的英语道歉。 我在 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/

相关文章:

mysql - 在mysql中返回具有重复列值的记录

mysql - SQOOP:表导入错误

sql - 在 TIMESTAMP 类型的 MySQL 列中使用零值

jquery - 如何使用 Jquery 从 UL 列表中只选择悬停的项目

php - 将 MySQLi 与 PDO 一起使用?

python - 由于字符串中的单引号或双引号而导致 SQL 语法错误

mysql - 关系型数据库设计(MySQL)

MySQL 如果差异小于 0,则选择 0,否则选择差异

mysql - 如何在 Ubuntu 中将 .sql 文件导入 PostgreSQL 数据库

sql - SQL 中处理 INT 数据类型字段中无值和零值之间差异的正确方法