MySQL 使用select语句替换sql中的空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9877533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace nulls values in sql using select statement?
提问by Thompson
Ho to do this? What query can be written by using selectstatement where all nulls should be replaced with 123?
何做这个?使用select语句可以编写什么查询,其中所有空值都应替换为 123?
I know we can do this y using, update tablename set fieldname = "123" where fieldname is null;
我知道我们可以使用 update tablename set fieldname = "123" where fieldname is null;
but can't do it using selectstatement.
但不能使用select语句来做到这一点。
回答by RedFilter
You have a lot of options for substituting NULL values in MySQL:
在 MySQL 中替换 NULL 值有很多选择:
select case
when fieldname is null then '123'
else fieldname end as fieldname
from tablename
select coalesce(fieldname, '123') as fieldname
from tablename
select ifnull(fieldname, '123') as fieldname
from tablename
回答by Dave Halter
There is a statement called IFNULL, which takes all the input values and returns the first non NULL value.
有一个名为 IFNULL 的语句,它接受所有输入值并返回第一个非 NULL 值。
example:
例子:
select IFNULL(column, 1) FROM table;
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull
回答by LesterDove
I think you're looking for the IFNULL function
IFNULL(field, 0)
will return a 0 when the field returns null
我认为您正在寻找 IFNULL 函数
IFNULL(field, 0)
将在字段返回 null 时返回 0
回答by Exupery
An UPDATE statement is needed to update data in a table. You cannot use the SELECT statement to do so.
需要 UPDATE 语句来更新表中的数据。您不能使用 SELECT 语句来执行此操作。