MySQL uses if to judge

Source: Internet
Author: User

Select *,if (sva=1, "male", "female") as Ssva from Taname where sva<> ""

12.2. Control process functions
Case value when [compare-value] and then result [when [compare-value] then result ...] [ELSE result] END case [condition] then result [when [condition] then result ...] [ELSE result] END
In the return result of the first scenario, Value=compare-value. The return result of the second scenario is the real result of the first case. If there is no matching result value, the result is returned with the else result, and if there is no else part, the return value is NULL.

Mysql> SELECT Case 1 while 1 then one

-If 2 then the ELSE more END;

-One

Mysql> SELECT case is 1>0 then true ELSE false END;

-True

mysql> SELECT Case BINARY B

When a and then 1 is B then 2 END;

, NULL

The default return value type of a case expression is a compatible collection type of any return value, but depends on the context in which it is located. If used in the context of a string, the resulting flavor string is returned. If used in a digital context, the result is a decimal value, a real value, or an integer value.

IF (EXPR1,EXPR2,EXPR3)
If Expr1 is true (expr1 <> 0 and Expr1 <> NULL), then the return value of if () is expr2; Otherwise the return value is EXPR3. The return value of IF () is a numeric value or a string value, depending on the context in which it is located.

Mysql> SELECT IF (1>2,2,3);

3

Mysql> SELECT IF (1<2,yes, no);

Yes

Mysql> SELECT IF (STRCMP (test,test1), no,yes);

-No

If only one of the EXPR2 or EXPR3 is explicitly NULL, the result type of the if () function is the result type of the non-NULL expression.

EXPR1 is calculated as an integer value, that is, if you are validating floating-point values or string values, you should use a comparison operation for validation.

Mysql> SELECT IF (0.1,1,0);

0

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

1

In the first example shown, the return value of if (0.1) is 0, because 0.1 is converted to an integer value, resulting in a test of if (0). This may not be the case you want. In the second example, the comparison examines the original floating-point value to see if it is a value other than 0. The comparison results use integers.

The default return value type of IF (), which is important when it is stored in a temporary table, is calculated as follows:

An expression
return value

The EXPR2 or EXPR3 return value is a string.
String

The EXPR2 or EXPR3 return value is a floating-point value.
Floating point

The EXPR2 or EXPR3 return value is an integer.
Integer

If Expr2 and EXPR3 are both strings, and any one of the strings is case-sensitive, the return result is case-sensitive.
http://blog.knowsky.com/
Ifnull (EXPR1,EXPR2)
If EXPR1 is not NULL, the return value of Ifnull () is expr1; Otherwise its return value is EXPR2. 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);

1

Mysql> SELECT ifnull (null,10);

10

Mysql> SELECT ifnull (1/0,10);

10

Mysql> SELECT ifnull (1/0,yes);

Yes

The default result value for Ifnull (EXPR1,EXPR2) is one of the more "common" in two expressions, in the order of string, real, or INTEGER. Suppose a case of an expression-based table, or MySQL must store the return value of Ifnull () in a temporary table in the internal memory:

CREATE TABLE tmp SELECT ifnull (1,test) as test;

In this example, the type of the test column is CHAR (4).

Nullif (EXPR1,EXPR2)
If Expr1 = EXPR2 is established, then the return value is NULL, otherwise the return value is EXPR1. This is the same as when the case is Expr1 = Expr2 then NULL ELSE expr1 end.

Mysql> SELECT Nullif ();

, NULL

Mysql> SELECT Nullif;

1

Note that if the arguments are not equal, the MySQL two-time value is EXPR1

MySQL uses if to judge

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.