如何在 MySQL 中检查字段是否为空或为空? [英] How to check if field is null or empty in MySQL?

查看:87
本文介绍了如何在 MySQL 中检查字段是否为空或为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何检查一个字段是 NULL 还是 empty.我有这个:

I am trying to figure out how to check if a field is NULL or empty. I have this:

SELECT IFNULL(field1, 'empty') as field1 from tablename

我需要添加一个额外的检查 field1 != "" 类似于:

I need to add an additional check field1 != "" something like:

SELECT IFNULL(field1, 'empty') OR field1 != ""  as field1 from tablename

知道如何做到这一点吗?

Any idea how to accomplish this?

推荐答案

要么使用

SELECT IF(field1 IS NULL or field1 = '', 'empty', field1) as field1 
from tablename

SELECT case when field1 IS NULL or field1 = ''
            then 'empty'
            else field1
       end as field1 
from tablename

如果您只想检查 null 而不是空字符串,那么您也可以使用 ifnull()coalesce(field1, 'empty').但这不适用于空字符串.

If you only want to check for null and not for empty strings then you can also use ifnull() or coalesce(field1, 'empty'). But that is not suitable for empty strings.

这篇关于如何在 MySQL 中检查字段是否为空或为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆