{ "data": { "question": { "questionId": "627", "questionFrontendId": "627", "categoryTitle": "Database", "boundTopicId": 1209, "title": "Swap Salary", "titleSlug": "swap-salary", "content": "
Table: Salary
\n+-------------+----------+\n| Column Name | Type |\n+-------------+----------+\n| id | int |\n| name | varchar |\n| sex | ENUM |\n| salary | int |\n+-------------+----------+\nid is the primary key (column with unique values) for this table.\nThe sex column is ENUM (category) value of type ('m', 'f').\nThe table contains information about an employee.\n\n\n
\n\n
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.
\n\nThe result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nSalary table:\n+----+------+-----+--------+\n| id | name | sex | salary |\n+----+------+-----+--------+\n| 1 | A | m | 2500 |\n| 2 | B | f | 1500 |\n| 3 | C | m | 5500 |\n| 4 | D | f | 500 |\n+----+------+-----+--------+\nOutput: \n+----+------+-----+--------+\n| id | name | sex | salary |\n+----+------+-----+--------+\n| 1 | A | f | 2500 |\n| 2 | B | m | 1500 |\n| 3 | C | f | 5500 |\n| 4 | D | m | 500 |\n+----+------+-----+--------+\nExplanation: \n(1, A) and (3, C) were changed from 'm' to 'f'.\n(2, B) and (4, D) were changed from 'f' to 'm'.\n\n", "translatedTitle": "变更性别", "translatedContent": "
Salary
表:
\n+-------------+----------+\n| Column Name | Type |\n+-------------+----------+\n| id | int |\n| name | varchar |\n| sex | ENUM |\n| salary | int |\n+-------------+----------+\nid 是这个表的主键(具有唯一值的列)。\nsex 这一列的值是 ENUM 类型,只能从 ('m', 'f') 中取。\n本表包含公司雇员的信息。\n\n\n
\n\n
请你编写一个解决方案来交换所有的 'f'
和 'm'
(即,将所有 'f'
变为 'm'
,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。
注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。
\n\n结果如下例所示。
\n\n\n\n
示例 1:
\n\n\n输入:\nSalary 表:\n+----+------+-----+--------+\n| id | name | sex | salary |\n+----+------+-----+--------+\n| 1 | A | m | 2500 |\n| 2 | B | f | 1500 |\n| 3 | C | m | 5500 |\n| 4 | D | f | 500 |\n+----+------+-----+--------+\n输出:\n+----+------+-----+--------+\n| id | name | sex | salary |\n+----+------+-----+--------+\n| 1 | A | f | 2500 |\n| 2 | B | m | 1500 |\n| 3 | C | f | 5500 |\n| 4 | D | m | 500 |\n+----+------+-----+--------+\n解释:\n(1, A) 和 (3, C) 从 'm' 变为 'f' 。\n(2, B) 和 (4, D) 从 'f' 变为 'm' 。\n
\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"Salary\":[\"id\",\"name\",\"sex\",\"salary\"]},\"rows\":{\"Salary\":[[1,\"A\",\"m\",2500],[2,\"B\",\"f\",1500],[3,\"C\",\"m\",5500],[4,\"D\",\"f\",500]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"