|
| 1 | +--627. Swap Salary |
| 2 | +-- |
| 3 | +--Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update statement and no intermediate temp table. |
| 4 | +-- |
| 5 | +--Note that you must write a single update statement, DO NOT write any select statement for this problem. |
| 6 | +-- |
| 7 | +-- |
| 8 | +--Example: |
| 9 | +-- |
| 10 | +--| id | name | sex | salary | |
| 11 | +--|----|------|-----|--------| |
| 12 | +--| 1 | A | m | 2500 | |
| 13 | +--| 2 | B | f | 1500 | |
| 14 | +--| 3 | C | m | 5500 | |
| 15 | +--| 4 | D | f | 500 | |
| 16 | + |
| 17 | +--After running your update statement, the above salary table should have the following rows: |
| 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 | + |
1 | 25 | --solution 1:
|
2 | 26 | update salary
|
3 | 27 | set sex=CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex));
|
|