본문 바로가기
SQL

[LeetCode] Swap Salary (update, ENUM, SET, CASE, IF)

by YGSEO 2021. 4. 9.
728x90

ENUM data type link

ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are sorted in the order you enter them

 

UPDATE salary SET sex = IF(sex='m', 'f', 'm');
UPDATE salary
SET
    sex = CASE sex
        WHEN 'm' THEN 'f'
        ELSE 'm'
    END;

SET Keyword

The SET command is used with UPDATE to specify which columns and values that should be updated in a table.

The following SQL updates the first customer (CustomerID = 1) with a new ContactName and a new City:

Example

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

CASE Statement (if - else 문이라고 생각하면 됨)

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

 

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END; 

IF() Function

Example

Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE:

SELECT IF(500<1000, "YES", "NO"); 

 

728x90

댓글