[英]Replacing null values in MySQL query in when clause
I am having a MySQL Table as One Below 我下面有一个MySQL表
CREATE TABLE Test(Name VARCHAR(30));
INSERT INTO Test VALUES ('Name1'),
('Name2'),
(null),
('Name3'),
(null),
('Name4');
Now when the row is Null I want it to be Displayed as Empty in front end else the value contained in the row 现在,当该行为Null时,我希望它在前端显示为空,否则该行中包含的值
I tried the following query but it ain't worked 我尝试了以下查询,但没有用
SELECT CASE Name
WHEN NULL
THEN '' ELSE Name
END AS Names
FROM Test
Output 输出量
Name1
Name2
Name3
Name4
The link for SQLFiddle SQLFiddle的链接
Thanks for Reply 谢谢您的回复
Null values are special in SQL, you can't treat them as ordinary expressions. 空值在SQL中是特殊的,您不能将它们视为普通表达式。 Although you can assign NULL
to a column, you can't compare with it as if it's a normal value; 尽管您可以将NULL
分配给列,但是您不能像将其作为正常值一样与之进行比较。 all comparison functions return NULL
if either of the arguments is NULL
, and this is treated as false (so NULL = NULL
and 1 <> NULL
are both false). 如果两个参数中的任何一个为NULL
,则所有比较函数都将返回NULL
,并将其视为false(因此NULL = NULL
和1 <> NULL
均为false)。 You're required to use special expressions to compare with NULL
. 您需要使用特殊表达式与NULL
进行比较。 The most basic form of this is <expression> IS NULL
(or <expression> IS NOT NULL
), but there are also convenience functions IFNULL()
and COALESCE()
. 最基本的形式是<expression> IS NULL
(或<expression> IS NOT NULL
),但是还有便捷函数IFNULL()
和COALESCE()
。
SELECT IFNULL(Name, '') AS Names
FROM Test
or 要么
SELECT CASE WHEN Name IS NULL
THEN ''
ELSE Name
END AS Names
FROM Test
For more information, see 3.3.4.6. 有关更多信息,请参见3.3.4.6。 Working with NULL Values in the MySQL documentation. 在MySQL文档中使用NULL值 。
Just to elaborate on why this doesn't work. 只是要详细说明为什么它不起作用。
A NULL is a value for which comparison yields neither a true nor false result. NULL是一个值,比较不会产生真或假结果。 That's one of the oddities of the three-tiered logic intrinsic to database programming. 这是数据库编程固有的三层逻辑的怪异之一。
NULL = NULL
is not true. 是不正确的。 It's not false either. 这也不是错误的。 The outcome of any comparison of one value to a NULL value is a NULL result, unless you use the intrinsic SQL " IS NULL
" operator to test it. 将一个值与NULL值进行任何比较的结果均为NULL结果,除非您使用内部SQL“ IS NULL
”运算符对其进行测试。
In the form of CASE statement you were using: 以CASE语句的形式,您正在使用:
CASE Value
WHEN TestValue1 THEN xxx
WHEN TestValue2 THEN yyy
END
a number of equality tests are run against a value. 针对一个值运行许多相等性测试。 You cannot test for NULL in this form, because you cannot do equality testing for NULL. 您不能以这种形式测试NULL,因为您不能对NULL进行相等测试。 The second form of case: 案例的第二种形式:
CASE
WHEN MyValue IS NULL THEN www
WHEN Condition1 THEN xxx
WHEN Condition2 THEN yyy
END
does allow you to use an IS NULL
expression, which can have a TRUE or FALSE value. 确实允许您使用IS NULL
表达式,该表达式可以具有TRUE或FALSE值。 Thus, when you want to do NULL testing in a CASE statement, you must always use this form. 因此,当您想在CASE语句中执行NULL测试时,必须始终使用此形式。
Also, the NULL test should be the first item in the CASE statement: because comparing any value against NULL will have an "undetermined" (NULL) boolean value, and the logic of the case will not work properly otherwise. 另外,NULL测试应该是CASE语句中的第一项:因为将任何值与NULL进行比较都会有一个“不确定”(NULL)布尔值,否则,案例逻辑将无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.