Mysql-isnull (), Ifnull (), and Nullif () functions

Source: Internet
Author: User

The following three functions can be used for where sub-conditions, as data deletion, updated record positioning basis.

Such as:

SELECT * from Usergrade WHERE ISNULL (USERNAME);

One, ISNULL (expr)

If expr is null, then the return value of IsNull () is 1, otherwise the return value is 0.

 mysql> select  isnull (1 /0 ) as   result;  +--------+|      Result |+--------+| 1  |+--------+1  row in  set  (0.00   sec) MySQL  > select  isnull (0 / 1 ) as   result;  +--------+|      Result |+--------+| 0  |+--------+1  row in  set  (0.00  sec) 

The comparison of NULL values using = is usually wrong.

The IsNull () function has some of the same characteristics as the IS null comparison operator. See the description for is null.

Second, ifnull (EXPR1,EXPR2)

If EXPR1 is not NULL, the return value of EXPR2 is EXPR2, regardless of whether the value of Null,ifnull () is expr1;

The return value of Ifnull () is either a number or a string, depending on the context in which it is used.

Mysql> SELECT Ifnull (1,0);+-------------+| Ifnull (1,0) |+-------------+|1|+-------------+1Rowinch Set(0.01sec) MySQL> SELECT ifnull (NULL,Ten); +-----------------+| Ifnull (NULL,Ten) |+-----------------+|Ten|+-----------------+1Rowinch Set(0.00sec) MySQL> SELECT ifnull (1/0,Ten); +----------------+| Ifnull (1/0,Ten) |+----------------+|10.0000|+----------------+1Rowinch Set(0.00sec) MySQL> SELECT ifnull (1/0,'Yes'); +-------------------+| Ifnull (1/0,'Yes') |+-------------------+| Yes |+-------------------+1Rowinch Set(0.00sec) MySQL> SELECT ifnull (1,'Yes'); +-----------------+| Ifnull (1,'Yes') |+-----------------+|1|+-----------------+1Rowinch Set(0.00Sec

Iii. Nullif (expr)

If EXPR1=EXPR2 is true, the return value is NULL, otherwise the return value is EXPR1.

Mysql>SelectNullif (1,1);+-------------+| Nullif (1,1) |+-------------+| NULL |+-------------+1Rowinch Set(0.00sec) MySQL>SelectNullif ('a',1);+---------------+| Nullif ('a',1) |+---------------+| A |+---------------+1Rowinch Set,1Warning (0.00Sec

Mysql-isnull (), Ifnull (), and Nullif () functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.