提交 fd30a1a2 编写于 作者: M Mars Liu

double not exists problem

上级 c51938f9
{ {
"node_id": "mysql-af89d54d66974111b4f67aeba2af7161", "node_id": "mysql-af89d54d66974111b4f67aeba2af7161",
"keywords": [], "keywords": ["engine", "innodb", "myisam"],
"children": [], "children": [],
"export": [], "export": [],
"keywords_must": [] "keywords_must": []
......
{
"type": "code_options",
"author": "Mars Liu",
"source": "DoubleNotExists.md",
"notebook_enable": false,
"exercise_id": "2fabb69f22224e48a26fec8911798ceb"
}
\ No newline at end of file
# Double Not Exists Check
风控部们的数据库中有一组关于招投标的数据,关键信息如下:
```mysql
-- 招标项目
create table invitation
(
id int primary key auto_increment,
name varchar(256),
attachment text
-- ignore more detail ...
);
-- 竞标企业
create table company
(
id int primary key auto_increment,
name varchar(256)
-- ignore more detail ...
);
-- 投标记录
create table bids
(
id int primary key auto_increment,
invitation_id int references invitation (id),
company_id int references company (id)
-- ignore more detail ...
);
```
Joe 想要找出参与了所有投标的企业(有围标嫌疑),那么这个查询应该怎么写?
## 答案
```mysql
select *
from company
where not exists(
select *
from invitation
where not exists(
select *
from bids
where invitation.id = bids.invitation_id
and bids.company_id = company.id
)
)
```
## 选项
### A
```mysql
select *
from company
where id = all (select company_id
from bids)
```
### B
```mysql
select *
from company
where id = any (select company_id
from bids)
```
### C
```mysql
select *
from company
where company.id in (select distinct company_id from bids)
```
### D
```mysql
select *
from company
where exists(
select *
from bids
where bids.company_id = company.id
)
```
\ No newline at end of file
{
"node_id": "mysql-72761d60ad7c4a9a9c87435daca246b9",
"keywords": [
"exists",
"not exists",
"double not exists"
],
"children": [],
"export": [
"DoubleNotExists.json"
],
"keywords_must": [],
"keywords_forbid": [],
"groups": 2
}
\ No newline at end of file
{
"node_id": "mysql-bee8db3dd5354a42bb50277fe4cb2913",
"keywords": [],
"children": [],
"export": [
"customer_order.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 2
}
\ No newline at end of file
{ {
"node_id": "mysql-a2ddae1b044149ecbb74db3b6eb32721", "node_id": "mysql-a2ddae1b044149ecbb74db3b6eb32721",
"keywords": [], "keywords": ["middle", "中间表"],
"children": [], "children": [],
"export": [], "export": [],
"keywords_must": [], "keywords_must": [],
......
...@@ -93,7 +93,12 @@ ...@@ -93,7 +93,12 @@
{ {
" 创建和删除数据库": { " 创建和删除数据库": {
"node_id": "mysql-764d5080ddb943fe9236922984afa152", "node_id": "mysql-764d5080ddb943fe9236922984afa152",
"keywords": [], "keywords": [
"create database",
"drop database",
"创建数据库",
"删除数据库"
],
"children": [], "children": [],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [] "keywords_forbid": []
...@@ -120,7 +125,10 @@ ...@@ -120,7 +125,10 @@
{ {
"存储引擎": { "存储引擎": {
"node_id": "mysql-cd45ce715f914ef2895df639a5d206a3", "node_id": "mysql-cd45ce715f914ef2895df639a5d206a3",
"keywords": [], "keywords": [
"engine",
"存储引擎"
],
"children": [], "children": [],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [] "keywords_forbid": []
...@@ -483,15 +491,6 @@ ...@@ -483,15 +491,6 @@
"keywords_forbid": [] "keywords_forbid": []
} }
}, },
{
"存储引擎": {
"node_id": "mysql-af89d54d66974111b4f67aeba2af7161",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
},
{ {
" 触发器": { " 触发器": {
"node_id": "mysql-d1d26b008711441f82192c33e1fe4dd7", "node_id": "mysql-d1d26b008711441f82192c33e1fe4dd7",
...@@ -529,7 +528,10 @@ ...@@ -529,7 +528,10 @@
{ {
" 别名": { " 别名": {
"node_id": "mysql-1d350c6226d443bdb76b5058d8ee23e7", "node_id": "mysql-1d350c6226d443bdb76b5058d8ee23e7",
"keywords": [], "keywords": [
"aliases",
"别名"
],
"children": [], "children": [],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [] "keywords_forbid": []
...@@ -640,7 +642,10 @@ ...@@ -640,7 +642,10 @@
{ {
" 求和": { " 求和": {
"node_id": "mysql-c5654c150993418a96f692496837fbb7", "node_id": "mysql-c5654c150993418a96f692496837fbb7",
"keywords": [], "keywords": [
"sum",
"求和"
],
"children": [], "children": [],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [] "keywords_forbid": []
...@@ -1766,6 +1771,15 @@ ...@@ -1766,6 +1771,15 @@
"keywords_forbid": [] "keywords_forbid": []
} }
}, },
{
" 范式设计": {
"node_id": "mysql-bee8db3dd5354a42bb50277fe4cb2913",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
},
{ {
"反范式设计": { "反范式设计": {
"node_id": "mysql-3e4c837b471c454c90bfbc32445f2780", "node_id": "mysql-3e4c837b471c454c90bfbc32445f2780",
......
create table invitation
(
id int primary key auto_increment,
name varchar(256),
attachment text
-- ignore more detail ...
);
create table company(
id int primary key auto_increment,
name varchar(256)
-- ignore more detail ...
);
create table bids
(
id int primary key auto_increment,
invitation_id int references invitation(id),
company_id int references company(id)
-- ignore more detail ...
);
-- double not exists sample
select *
from company
where not exists(
select *
from invitation
where not exists(
select *
from bids
where invitation.id = bids.invitation_id
and bids.company_id = company.id
)
)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册