최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰

■ Database ■/MySQL

IF, ISNULL

serpiko 2018. 11. 22. 11:52


   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");
Try it Yourself »

Definition and Usage

The IF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.

Syntax

IF(conditionvalue_if_truevalue_if_false)

Example

Return 5 if the condition is TRUE, or 10 if the condition is FALSE:

SELECT IF(500<1000510);
Try it Yourself »

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");
Try it Yourself »

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;
Try it Yourself »

https://www.w3schools.com/sql/func_mysql_if.asp




   ISNULL


Example

Test whether an expression is NULL:

SELECT ISNULL(NULL);
Try it Yourself »

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

ParameterDescription
expressionRequired. The value to test

Technical Details

Works in:From MySQL 4.0

More Examples

Example

Test whether an expression is NULL:

SELECT ISNULL("");
Try it Yourself »

Example

Test whether an expression is NULL:

SELECT ISNULL(350);
Try it Yourself »

Example

Test whether an expression is NULL:

SELECT ISNULL("Hello world!");
Try it Yourself »

https://www.w3schools.com/sql/func_mysql_isnull.asp




   SQL)


1
IF( ISNULL(B.domain_id) = 1'no''yes' ) as active
cs


댓글