|
| 1 | +<p>Table: <code>Salary</code></p> |
| 2 | + |
| 3 | +<pre> |
| 4 | ++-------------+----------+ |
| 5 | +| Column Name | Type | |
| 6 | ++-------------+----------+ |
| 7 | +| id | int | |
| 8 | +| name | varchar | |
| 9 | +| sex | ENUM | |
| 10 | +| salary | int | |
| 11 | ++-------------+----------+ |
| 12 | +id is the primary key (column with unique values) for this table. |
| 13 | +The sex column is ENUM (category) value of type ('m', 'f'). |
| 14 | +The table contains information about an employee. |
| 15 | +</pre> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | + |
| 19 | +<p>Write a solution to swap all <code>'f'</code> and <code>'m'</code> values (i.e., change all <code>'f'</code> values to <code>'m'</code> and vice versa) with a <strong>single update statement</strong> and no intermediate temporary tables.</p> |
| 20 | + |
| 21 | +<p>Note that you must write a single update statement, <strong>do not</strong> write any select statement for this problem.</p> |
| 22 | + |
| 23 | +<p>The result format is in the following example.</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> |
| 30 | +Salary table: |
| 31 | ++----+------+-----+--------+ |
| 32 | +| id | name | sex | salary | |
| 33 | ++----+------+-----+--------+ |
| 34 | +| 1 | A | m | 2500 | |
| 35 | +| 2 | B | f | 1500 | |
| 36 | +| 3 | C | m | 5500 | |
| 37 | +| 4 | D | f | 500 | |
| 38 | ++----+------+-----+--------+ |
| 39 | +<strong>Output:</strong> |
| 40 | ++----+------+-----+--------+ |
| 41 | +| id | name | sex | salary | |
| 42 | ++----+------+-----+--------+ |
| 43 | +| 1 | A | f | 2500 | |
| 44 | +| 2 | B | m | 1500 | |
| 45 | +| 3 | C | f | 5500 | |
| 46 | +| 4 | D | m | 500 | |
| 47 | ++----+------+-----+--------+ |
| 48 | +<strong>Explanation:</strong> |
| 49 | +(1, A) and (3, C) were changed from 'm' to 'f'. |
| 50 | +(2, B) and (4, D) were changed from 'f' to 'm'. |
| 51 | +</pre> |
0 commit comments