any.md 914 字节
Newer Older
M
all  
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
# ANY

Joe 想要从员工表

```mysql
create table employee(
    id int primary key auto_increment,
    name varchar(256),
    dept varchar(256),
    salary decimal(12, 4)
);
```

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

## 答案

```mysql
select id, name, dept, salary
from employee as o
where o.salary < any(select salary from employee as i where i.dept=o.dept)
```

## 选项

### A

```mysql
select id, name, dept, salary
from employee as o
join employee as i on o.dept = i.dept and o.salary < i.salary
```

### B

```mysql
select o.id, o.name, o.dept, o.salary
from employee as o
left join employee as i on o.dept = i.dept and o.salary < i.salary
where i.id is null;
```

### C

```mysql
select o.id, o.name, o.dept, o.salary 
from employee as o 
    left join employee as i on o.dept = i.dept and o.salary < i.salary 
where i.id is not null;
```