返回介绍

solution / 0600-0699 / 0627.Swap Salary / README_EN

发布于 2024-06-17 01:03:36 字数 2370 浏览 0 评论 0 收藏 0

627. Swap Salary

中文文档

Description

Table: Salary

+-------------+----------+
| Column Name | Type   |
+-------------+----------+
| id      | int    |
| name    | varchar  |
| sex     | ENUM   |
| salary    | int    |
+-------------+----------+
id is the primary key (column with unique values) for this table.
The sex column is ENUM (category) value of type ('m', 'f').
The table contains information about an employee.

 

Write a solution to 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 temporary tables.

Note that you must write a single update statement, do not write any select statement for this problem.

The result format is in the following example.

 

Example 1:

Input: 
Salary table:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1  | A  | m   | 2500   |
| 2  | B  | f   | 1500   |
| 3  | C  | m   | 5500   |
| 4  | D  | f   | 500  |
+----+------+-----+--------+
Output: 
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1  | A  | f   | 2500   |
| 2  | B  | m   | 1500   |
| 3  | C  | f   | 5500   |
| 4  | D  | m   | 500  |
+----+------+-----+--------+
Explanation: 
(1, A) and (3, C) were changed from 'm' to 'f'.
(2, B) and (4, D) were changed from 'f' to 'm'.

Solutions

Solution 1

UPDATE salary
SET sex = CASE sex
  WHEN 'm' THEN 'f'
  ELSE 'm'
END;

Solution 2

# Write your MySQL query statement below
UPDATE Salary
SET sex = IF(sex = 'f', 'm', 'f');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文