Skip to content

Commit cdc2bb4

Browse files
committed
[LeetCode Sync] Runtime - 207 ms (99.31%), Memory - 0.0B (100.00%)
1 parent a7ca12e commit cdc2bb4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 (&#39;m&#39;, &#39;f&#39;).
14+
The table contains information about an employee.
15+
</pre>
16+
17+
<p>&nbsp;</p>
18+
19+
<p>Write a solution to swap all <code>&#39;f&#39;</code> and <code>&#39;m&#39;</code> values (i.e., change all <code>&#39;f&#39;</code> values to <code>&#39;m&#39;</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>&nbsp;</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 &#39;m&#39; to &#39;f&#39;.
50+
(2, B) and (4, D) were changed from &#39;f&#39; to &#39;m&#39;.
51+
</pre>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
UPDATE Salary
2+
SET sex = CASE sex
3+
WHEN 'm' THEN 'f'
4+
ELSE 'm'
5+
END

0 commit comments

Comments
 (0)