Skip to content

Commit 05d5965

Browse files
committed
Add solution 627 [sql]
if function() case when .. then .. else .. end
1 parent 22b2294 commit 05d5965

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
5151
| 595 | [Big Countries](https://github.com/doocs/leetcode/tree/master/solution/595.Big%20Countries) | `SQL` |
5252
| 596 | [Classes More Than 5 Students](https://github.com/doocs/leetcode/tree/master/solution/596.Classes%20More%20Than%205%20Students) | `SQL` |
5353
| 605 | [Can Place Flowers](https://github.com/doocs/leetcode/tree/master/solution/605.Can%20Place%20Flowers) | `Array` |
54+
| 627 | [Swap Salary](https://github.com/doocs/leetcode/tree/master/solution/627.Swap%20Salary) | `SQL` |
5455
| 695 | [Max Area of Island](https://github.com/doocs/leetcode/tree/master/solution/695.Max%20Area%20of%20Island) | `Array`, `Depth-first Search` |
5556
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
5657
| 707 | [Design Linked List](https://github.com/doocs/leetcode/tree/master/solution/707.Design%20Linked%20List) | `Linked List`, `Design` |

solution/627.Swap Salary/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## 交换工资
2+
### 题目描述
3+
4+
给定一个 `salary` 表,如下所示,有m=男性 和 f=女性的值 。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求使用一个更新查询,并且没有中间临时表。
5+
6+
例如:
7+
```
8+
| id | name | sex | salary |
9+
|----|------|-----|--------|
10+
| 1 | A | m | 2500 |
11+
| 2 | B | f | 1500 |
12+
| 3 | C | m | 5500 |
13+
| 4 | D | f | 500 |
14+
```
15+
16+
运行你所编写的查询语句之后,将会得到以下表:
17+
```
18+
| id | name | sex | salary |
19+
|----|------|-----|--------|
20+
| 1 | A | f | 2500 |
21+
| 2 | B | m | 1500 |
22+
| 3 | C | f | 5500 |
23+
| 4 | D | m | 500 |
24+
```
25+
26+
### 解法
27+
使用 `if` 函数 或者 `case when .. then .. else ... end`
28+
29+
```sql
30+
# Write your MySQL query statement below
31+
# update salary
32+
# set sex = if(sex = 'm', 'f', 'm')
33+
34+
update salary
35+
set sex = (case when sex = 'f' then 'm' else 'f' end)
36+
37+
```
38+
39+
#### Input
40+
```json
41+
{"headers":{"salary":["id","name","sex","salary"]},"rows":{"salary":[[1,"A","m",2500],[2,"B","f",1500],[3,"C","m",5500],[4,"D","f",500]]}}
42+
```
43+
44+
#### Output
45+
```json
46+
{"headers":["id","name","sex","salary"],"values":[[1,"A","f",2500],[2,"B","m",1500],[3,"C","f",5500],[4,"D","m",500]]}
47+
```

0 commit comments

Comments
 (0)