
Problem

Example

Thinking
sex의 값을 m일 경우 f 로, f 일 경우 m으로 update를 해야한다.
하나의 값을 변경할 경우에는 update 테이블 set 컬럼 = 변경할 값 으로 하면 되는 데,
여기서는 조건이 2개이다. 그럴 경우에는 이 방법을 사용하자!
UPDATE 테이블명
SET 컬럼명 =
CASE
WHEN 컬럼명1 = 기존값1 THEN 변경할 값1
WHEN 컬럼명2 = 기존값2 THEN 변경할 값2
ELSE 변경할 값3
END;
IF문 처럼 원하는 만큼 조건 절을 WHEN ~ THEN에 넣어주면 된다.
그리고 위에 조건을 모두 충족하지 않을 시 반환할 값을 ELSE 에 넣어주고
마지막을 END로 마무리하기
Solve
제출 답안
update Salary
set sex = CASE WHEN sex = 'm' THEN 'f' ELSE 'm' END

https://leetcode.com/problems/swap-salary/submissions/
Swap Salary - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'💻 leetcode > database' 카테고리의 다른 글
[LeetCode] Database | 596. Classes More Than 5 Students (0) | 2022.01.12 |
---|---|
[LeetCode] Database | 197. Rising Temperature (0) | 2022.01.12 |
[LeetCode] Database | 196. Delete Duplicate Emails (0) | 2022.01.03 |
[LeetCode] Database | 183. Customers Who Never Order (0) | 2022.01.02 |
[LeetCode] Database | 182. Duplicate Emails (0) | 2022.01.02 |