MySQL View check if data is NULL

妖精的绣舞 提交于 2019-12-10 13:40:09

问题


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.


回答1:


You mean something like this?

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

There's also "IFNULL()":

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



回答2:


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

(doc)




回答3:


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

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.



来源:https://stackoverflow.com/questions/1174850/mysql-view-check-if-data-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!