MySQL View 检查数据是否为 ​​NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1174850/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:43:34  来源:igfitidea点击:

MySQL View check if data is NULL

mysqlselectviewcaseif-statement

提问by joeslice

I need to put a Case in the Select to check if the Data I'm adding to my view is NULL, in which case I want it to just enter a zero, or not.

我需要在 Select 中放置一个 Case 以检查我添加到视图中的数据是否为 ​​NULL,在这种情况下我希望它只输入零,或者不输入零。

回答by Adam Bradley

You mean something like this?

你的意思是这样的?

SELECT IF(`field` IS NULL, 0, `field`)...

There's also "IFNULL()":

还有“IFNULL()”:

SELECT IFNULL(`field`, 0)...

回答by joeslice

select coalesce(field, 0) as 'field' from v;

(doc)

(文档)

回答by Jacob Wyke

When creating your table just add NOT NULLto the column description, e.g.

创建表格时,只需添加NOT NULL到列描述中,例如

CREATE TABLE ( ID INT NOT NULL default '0' );

CREATE TABLE ( ID INT NOT NULL default '0' );

Then if no data is given for the field it is set to the default value of 0 which will be retrieved when you run a SELECT query.

然后,如果没有为该字段提供数据,则将其设置为默认值 0,在您运行 SELECT 查询时将检索该值。