Select *, if (SVA = 1, "male", "female") as ssva from tableame where id = 1
Quote control flow Functions
Case value when [compare-value] Then result [when [compare-value] Then result...] [else result] end case when [condition] Then result [when [condition] Then result...] [else result] End
In the returned results of the first scheme, value = compare-value. The returned results of the second solution are the real results of the first case. If no matching result value exists, the result after else is returned. If no else part exists, the return value is null.
Mysql> select case 1 when 1 then 'one'
-> When 2 then 'two' else 'more' end;
-> 'One'
Mysql> select case when 1> 0 then 'true' else 'false' end;
-> 'True'
Mysql> select case binary 'B'
-> When 'a 'then 1 when 'B' then 2 end;
-> Null
The default Return Value Type of a case expression is the compatible set type of any return value, but the specific situation depends on the context. If it is used in a string context, a result flavor string is returned. If it is used in the numeric context, the returned result is a decimal value, a real value, or an integer.
If (expr1, expr2, expr3)
If expr1 is true (expr1 <> 0 and expr1 <> null), the return value of IF () is expr2; otherwise, the return value is expr3. The return value of IF () is a numeric or string value, depending on the context.
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 expr2 or expr3 is null, The result type of the IF () function is not the result type of the null expression.
Expr1 is calculated as an integer. That is to say, if you are verifying a floating point or string value, you should use a comparison operation for testing.
Mysql> select if (0.1 );
-> 0
Mysql> select if (0.1 <>, 0 );
-> 1
In the first example, if (0.1) returns 0 because 0.1 is converted to an integer, resulting in a test of IF (0. This may not be what you want. In the second example, a comparison checks the original floating point value to see if it is a non-zero value. The comparison result uses an integer.
The default Return Value Type of IF () (which is important when it is stored in a temporary table) is calculated as follows:
Expression
Return Value
The return value of expr2 or expr3 is a string.
String
The return value of expr2 or expr3 is a floating point value.
Floating Point
The return value of expr2 or expr3 is an integer.
Integer
If both expr2 and expr3 are strings, and any one of them is case sensitive, the returned result is case sensitive.
Ifnull (expr1, expr2)
If expr1 is not null, the returned value of ifnull () is expr1; otherwise, the returned value is expr2. The returned value of ifnull () is a number or 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 of ifnull (expr1, expr2) is one of the two expressions that is more "common" in the order of string, real, or integer. Assume that an expression-based table or MySQL must store the returned value of ifnull () in a temporary table in internal memory:
Create Table TMP select ifnull (1, 'test') as test;
In this example, the test column type is Char (4 ).
Nullif (expr1, expr2)
If expr1 = expr2 is true, the return value is null. Otherwise, the return value is expr1. This is the same as case when expr1 = expr2 then null else expr1 end.
Mysql> select nullif (1, 1 );
-> Null
Mysql> select nullif (1, 2 );
-> 1
Note: If the parameters are not equal, the value obtained by MySQL twice is expr1.
Select *, if (SVA = 1, "male", "female") as ssva from tableame where id = 1
Quote control flow Functions
Case value when [compare-value] Then result [when [compare-value] Then result...] [else result] end case when [condition] Then result [when [condition] Then result...] [else result] End
In the returned results of the first scheme, value = compare-value. The returned results of the second solution are the real results of the first case. If no matching result value exists, the result after else is returned. If no else part exists, the return value is null.
Mysql> select case 1 when 1 then 'one'
-> When 2 then 'two' else 'more' end;
-> 'One'
Mysql> select case when 1> 0 then 'true' else 'false' end;
-> 'True'
Mysql> select case binary 'B'
-> When 'a 'then 1 when 'B' then 2 end;
-> Null
The default Return Value Type of a case expression is the compatible set type of any return value, but the specific situation depends on the context. If it is used in a string context, a result flavor string is returned. If it is used in the numeric context, the returned result is a decimal value, a real value, or an integer.
If (expr1, expr2, expr3)
If expr1 is true (expr1 <> 0 and expr1 <> null), the return value of IF () is expr2; otherwise, the return value is expr3. The return value of IF () is a numeric or string value, depending on the context.
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 expr2 or expr3 is null, The result type of the IF () function is not the result type of the null expression.
Expr1 is calculated as an integer. That is to say, if you are verifying a floating point or string value, you should use a comparison operation for testing.
Mysql> select if (0.1 );
-> 0
Mysql> select if (0.1 <>, 0 );
-> 1
In the first example, if (0.1) returns 0 because 0.1 is converted to an integer, resulting in a test of IF (0. This may not be what you want. In the second example, a comparison checks the original floating point value to see if it is a non-zero value. The comparison result uses an integer.
The default Return Value Type of IF () (which is important when it is stored in a temporary table) is calculated as follows:
Expression
Return Value
The return value of expr2 or expr3 is a string.
String
The return value of expr2 or expr3 is a floating point value.
Floating Point
The return value of expr2 or expr3 is an integer.
Integer
If both expr2 and expr3 are strings, and any one of them is case sensitive, the returned result is case sensitive.
Ifnull (expr1, expr2)
If expr1 is not null, the returned value of ifnull () is expr1; otherwise, the returned value is expr2. The returned value of ifnull () is a number or 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 of ifnull (expr1, expr2) is one of the two expressions that is more "common" in the order of string, real, or integer. Assume that an expression-based table or MySQL must store the returned value of ifnull () in a temporary table in internal memory:
Create Table TMP select ifnull (1, 'test') as test;
In this example, the test column type is Char (4 ).
Nullif (expr1, expr2)
If expr1 = expr2 is true, the return value is null. Otherwise, the return value is expr1. This is the same as case when expr1 = expr2 then null else expr1 end.
Mysql> select nullif (1, 1 );
-> Null
Mysql> select nullif (1, 2 );
-> 1
Note: If the parameters are not equal, the value obtained by MySQL twice is expr1.