File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ -- 627. Swap Salary
2+
3+ -- Given a table salary, such as the one below, that has m=male and f=female values.
4+ -- Swap all f and m values (i.e., change all f values to m and vice versa)
5+ -- with a single update query and no intermediate temp table.
6+
7+ -- For example:
8+
9+ -- | id | name | sex | salary |
10+ -- |----|------|-----|--------|
11+ -- | 1 | A | m | 2500 |
12+ -- | 2 | B | f | 1500 |
13+ -- | 3 | C | m | 5500 |
14+ -- | 4 | D | f | 500 |
15+
16+ -- After running your query, the above salary table should have the following rows:
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+ UPDATE salary
27+ SET
28+ sex = CASE sex
29+ WHEN ' m' THEN ' f'
30+ ELSE ' m'
31+ END;
You can’t perform that action at this time.
0 commit comments