The SQL problem encountered while brushing the Leetcode
627. Swap Salary
Given a table salary
, such as the one below, which 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 query and no intermediate tem P table.
For example:
| ID | name | sex | Salary | | ----|------|-----|--------|| 1 | A | m | 2500 | | 2 | B | f | | | 3 | C | m | 5500 | | 4 | D | f | |
After running your query, the above salary table should has the following rows:
| ID | name | sex | Salary | | ----|------|-----|--------|| 1 | A | f | 2500 | | 2 | B | m | | | | 3 | C | f | 5500 | | 4 | D | m | |
Probably asking for sex in the exchange table.
At first I wanted to use update salary set sex = f when sex = M again with an and and then the result of this implementation should be wrong,
And then just peek at the solution, just know there is a case statement, so record
Update Salary Set = Case Sex when ' m ' Then ' F ' Else ' m ' End
It means that when M,then is set to F,,else, it is set to M
SQL interchange values for data in a column (such as gender)