티스토리 뷰
IF, ISNULL
MYSQL에서 IF 와 ISNULL 을 조합하여 유용한 조건문 작성이 가능하다
IF
Example
Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE:
SELECT IF(500<1000, "YES", "NO");
Definition and Usage
The IF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.
Syntax
IF(condition, value_if_true, value_if_false)
Example
Return 5 if the condition is TRUE, or 10 if the condition is FALSE:
SELECT IF(500<1000, 5, 10);
Example
Test whether two strings are the same and return "YES" if they are, or "NO" if not:
SELECT IF(STRCMP("hello","bye") = 0, "YES", "NO");
Example
Return "MORE" if the condition is TRUE, or "LESS" if the condition is FALSE:
SELECT OrderID, Quantity, IF(Quantity>10, "MORE", "LESS")
FROM OrderDetails;
https://www.w3schools.com/sql/func_mysql_if.asp
ISNULL
Example
Test whether an expression is NULL:
SELECT ISNULL(NULL);
Definition and Usage
The ISNULL() function returns 1 or 0 depending on whether an expression is NULL.
If expression is NULL, this function returns 1. Otherwise, it returns 0.
Syntax
ISNULL(expression)
Parameter Values
Parameter | Description |
---|---|
expression | Required. The value to test |
Technical Details
Works in: | From MySQL 4.0 |
---|
More Examples
Example
Test whether an expression is NULL:
SELECT ISNULL("");
Example
Test whether an expression is NULL:
SELECT ISNULL(350);
Example
Test whether an expression is NULL:
SELECT ISNULL("Hello world!");
https://www.w3schools.com/sql/func_mysql_isnull.asp
SQL)
1 | IF( ISNULL(B.domain_id) = 1, 'no', 'yes' ) as active | cs |
'■ Database ■ > MySQL' 카테고리의 다른 글
원격접속을 위한 방화벽, 계정 설정 (0) | 2020.11.04 |
---|---|
concat과 time을 사용하여 date 쉽게 변환하기 (0) | 2018.11.13 |
date, date_format 을 사용한 날짜 비교 (0) | 2018.11.13 |
foreign key (0) | 2018.06.28 |
Mysql 접속 에러 Host is not allowed to connect to this Mysql server 혹은 Access denied for user (0) | 2018.03.16 |