The use of MYSQL ifnull functions

Source: Internet
Author: User

The Ifnull function is one of the most important functions in the MySQL database, and the following is an analysis of the use of the function, which I hope will help you.

The following is a detailed description of the use of MySQL ifnull function, for your reference to learn, if you have encountered similar problems in MySQL ifnull function use, may wish to see.

MYSQL ifnull (EXPR1,EXPR2)
If EXPR1 is not Null,ifnull () returns EXPR1, it returns EXPR2. Ifnull () returns a numeric or string value, depending on the context in which it is used.
Mysql> Select Ifnull (1,0);
1
Mysql> Select Ifnull (0,10);
0
Mysql> Select Ifnull (1/0,10);
10
Mysql> Select Ifnull (1/0,yes);
Yes

IF (EXPR1,EXPR2,EXPR3)
If Expr1 is true (expr1<>0 and Expr1<>null), then if () returns EXPR2, otherwise it returns EXPR3. IF () returns a numeric or string value, depending on the context in which it is used.
Mysql> Select IF (1>2,2,3);
3
Mysql> Select IF (1<2,yes,no);
Yes
Mysql> Select IF (strcmp (test,test1), yes,no);
-No

EXPR1 is calculated as an integer value, which means that if you are testing floating-point or string values, you should use a comparison operation to do so.

Mysql> Select IF (0.1,1,0);
0
Mysql> Select IF (0.1<>0,1,0);
1

In the first case above, if (0.1) returns 0, because 0.1 is transformed to an integer value, causing the test if (0). This may not be what you expect. In the second case, the comparison tests the original floating-point value to see if it is nonzero, and the result of the comparison is used as an integer.

Case value when [compare-value] and then result [when [compare-value] then result ...] [ELSE result] END

case is [condition] then result [when [condition] then result ...] [ELSE result] END
The first version returns a result, where value=compare-value. In the second version, if the first condition is true, a result is returned. If there is no matching result value, then the result after else is returned. If there is no else part, then NULL is returned.
Mysql> Select Case 1 while 1 then "one" while 2 then "one" and "more" END;
"One"
Mysql> Select Case is 1>0 then "true" ELSE "false" END;
"True"
mysql> Select Case BINARY ' B ' when ' a ' then 1 when ' B ' then 2 END;
, NULL

Use of the MYSQL ifnull function

Related Article

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.