MySQL in the case when pits for a NULL value determination

in the case when sql somewhat similar to the switch statement in Java, more flexible, but in Mysql in a little special treatment for Null

 

Mysql in the case when the syntax:

Syntax 1:

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

  

Syntax 2:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE

  

Note: Both the syntax is different, the differences are as follows:

1: The first syntax: case_value must be an expression, e.g. userid% 2 = 1 username is null, or the like. The syntax for this type of testing can not be NULL.

2: The second syntax is not required behind the CASE variable or expression, direct execution condition when evaluate each WHEN behind, if satisfied is executed.

 

The real case:

Table structure as follows: a value is null, b is 1

mysql> SELECT NULL AS a, 1 AS b;
+------+---+
| a    | b |
+------+---+
| NULL | 1 |
+------+---+

  

Now achieved if it takes a null value b value, or takes a value

Method 1: ifnull usage

The SELECT 
	the IFNULL (A, B) the AS new new, 
	A, 
	B 
the FROM 
	- create a temporary table: a value is null, b is. 1 
	(the SELECT A NULL the AS, the AS. 1 B) tmp;

  

 Method 2: case when usage

SELECT
	(
		CASE a
		WHEN a IS NULL THEN
			b
		ELSE
			a
		END
	) AS new,
	a,
	b
FROM
	(SELECT NULL AS a, 1 AS b) tmp;

  

The results obtained was found wrong, the new value is actually null, instead we want 1.

 

Why is there such a mistake? Is the first grammar and syntax mixed results in the second, the value case behind commission_pct in two ways: the true value or is null, while the back when commission_pct is null also has two values: true or false, so the back case to null time with true or false can never match, so the output is not null.

 

For this case, if you must use Syntax 1, then it can be rewritten as follows:

SELECT
	(
		CASE a IS NULL
		WHEN TRUE THEN b
		ELSE a			
		END
	) AS new,
	a,
	b
FROM
	(SELECT NULL AS a, 1 AS b) tmp;

  

Syntax 2 can also be used to write:

SELECT
	(
		CASE 
		WHEN a is NULL  THEN b	
		ELSE a	
		END
	) AS new,
	a,
	b
FROM
	(SELECT NULL AS a, 1 AS b) tmp;

  

Note that there may be another mistake is not easy to find errors in the case:

SELECT
	(
		CASE a 
		WHEN NULL THEN b
		ELSE a			
		END
	) AS new,
	a,
	b
FROM
	(SELECT NULL AS a, 1 AS b) tmp;

  

Seemed to have no problem, there are practical problems, questions reason is null judgment can not be judged by =. It simply is: the value used when the value of the back of the case expression syntax 1 = arbitrates, etc., but must use mysql is or is not.

 

to sum up:

1: Syntax 1 after the value of the expression follows the case calculate the "=" equal to the determination value with the use of the latter when condition equal to enter the branch.

2: Syntax case 2 is not required by an expression behind, to directly assess the condition when the value of the latter, if true proceeds.

 

Guess you like

Origin www.cnblogs.com/echojson/p/11612650.html