IF 조건문

1
2
3
4
5
6
7
8
9
10
11
-- 기본 사용법
IF (조건, 참일 때의 , 거짓일 때의 )

-- SELECT 문과 함께 사용
SELECT BOARD_ID, WRITER_ID, TITLE, PRICE,
IF (a.STATUS = 'SALE', '판매중',
    (IF (a.STATUS = 'DONE', '거래완료', 
			(IF (a.STATUS = 'RESERVED', '예약중', a.STATUS))))) AS STATUS
FROM USED_GOODS_BOARD AS a
WHERE CREATED_DATE ='2022-10-05'
ORDER BY BOARD_ID DESC;

CASE 조건문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- 기본 사용법
CASE
    WHEN (조건1) THEN 조건1에해당할때의값
    WHEN (조건2) THEN 조건2에해당할때의값
    WHEN (조건3) THEN 조건3에해당할때의값
END

-- SELECT 문과 함께 사용
SELECT BOARD_ID, WRITER_ID, TITLE, PRICE,
CASE
    WHEN (a.STATUS = 'SALE') THEN '판매중'
    WHEN (a.STATUS = 'DONE') THEN '거래완료'
    WHEN (a.STATUS = 'RESERVED') THEN '예약중'
    ELSE a.STATUS
END AS STATUS
FROM USED_GOODS_BOARD AS a
WHERE CREATED_DATE ='2022-10-05'
ORDER BY BOARD_ID DESC;

Reference

https://redcow77.tistory.com/260
https://www.next-t.co.kr/seo/sql/mysql-case-when-then-%EA%B5%AC%EB%AC%B8/