any.md 1.2 KB
Newer Older
M
all  
Mars Liu 已提交
1 2 3 4
# ANY

Joe 想要从员工表

fix bug  
张志晨 已提交
5
```sql
M
all  
Mars Liu 已提交
6 7 8
create table employee(
    id int primary key auto_increment,
    name varchar(256),
M
Mars Liu 已提交
9
    dept varchar(64),
M
all  
Mars Liu 已提交
10 11 12 13 14 15
    salary decimal(12, 4)
);
```

构造一个员工列表,排除每个部门最高工资的员工。这个查询可以怎样写?

M
Mars Liu 已提交
16 17
<hr/>

F
feilong 已提交
18
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill){target="_blank"}。
F
feilong 已提交
19

F
feilong 已提交
20 21
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
22

M
all  
Mars Liu 已提交
23 24
## 答案

fix bug  
张志晨 已提交
25
```sql
M
all  
Mars Liu 已提交
26 27
select id, name, dept, salary
from employee as o
28 29 30
where o.salary < any(select salary 
                     from employee as i 
                     where i.dept=o.dept)
M
all  
Mars Liu 已提交
31 32 33 34 35 36
```

## 选项

### A

fix bug  
张志晨 已提交
37
```sql
M
all  
Mars Liu 已提交
38 39
select id, name, dept, salary
from employee as o
40 41
join employee as i on 
    o.dept = i.dept and o.salary < i.salary
M
all  
Mars Liu 已提交
42 43 44 45
```

### B

fix bug  
张志晨 已提交
46
```sql
M
all  
Mars Liu 已提交
47 48
select o.id, o.name, o.dept, o.salary
from employee as o
49 50
left join employee as i on 
    o.dept = i.dept and o.salary < i.salary
M
all  
Mars Liu 已提交
51 52 53 54 55
where i.id is null;
```

### C

fix bug  
张志晨 已提交
56
```sql
M
all  
Mars Liu 已提交
57 58
select o.id, o.name, o.dept, o.salary 
from employee as o 
59 60
    left join employee as i 
        on o.dept = i.dept and o.salary < i.salary 
M
all  
Mars Liu 已提交
61 62
where i.id is not null;
```