MYSQL Ifnull Use Features

Source: Internet
Author: User
Tags strcmp

Later MySQL Ifnull uses the specific narrative of this feature for your reference to learn, assuming that your MySQL ifnull terms use functional category encounters a problem. Best-looking.

MYSQL ifnull (EXPR1,EXPR2)
Suppose Expr1 is not null,ifnull () returns EXPR1. Otherwise 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)
Assuming that 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    
   The   
Expr1 is calculated as an integer value. It means that you are testing a floating-point or string value. You should use a more action to do it.

         
    
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, which results in a test if (0). This may not be what you expect. In another case, compare the original floating-point value to see if it is non-zero. The comparative result is used as an integer.

         
    
Case Value when [ Compare-value] 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 number returns a result. Among the Value=compare-value. The second version number assumes that the first condition is true and returns result. If there is no matching result value, then the result after else is returned.

Assume 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

The following is a detailed description of the use of MySQL ifnull functions for your reference. Let's say you have a similar problem with MySQL ifnull function use, it's best to look at it.

MYSQL ifnull (EXPR1,EXPR2)
Suppose Expr1 is not null,ifnull () returns EXPR1, otherwise 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)
Assuming that 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 a floating-point or string value, you should use a comparison operation to do it.



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. Since 0.1 is transformed to an integer value, it results in a test if (0). This may not be what you expect. In another case, comparing the original floating-point value to see if it is nonzero, the comparative result 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 number returns a result. Among the Value=compare-value. The second version number assumes that the first condition is true. Returns the result. If there is no matching result value, then the result after else is returned. Assuming 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

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

MYSQL Ifnull Use Features

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.