检查字段是否为空 [英] Check field if empty

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

问题描述

如果我想检查该字段是否包含除null和empty以外的其他字符,查询是否正确?

Is the query correct if I wanted to check if the field has other characters other than null and empty?

select CASE WHEN description IS NULL THEN 'null'
  WHEN description IS NOT NULL THEN 'not null' ELSE 'something else' END 
  AS 'description'from milestone where name like '%Test%' or name like '%test%';

+-------------+
| description |
+-------------+
| not null    |
+-------------+
1 row in set (0.00 sec)

推荐答案

null和empty是否表示NULL +''(空字符串)?

Null and empty means NULL + '' (empty string)?

select CASE WHEN description IS NULL or description = '' THEN 'null or empty'
  ELSE 'not null' END 
  AS 'description'

在您的原始查询中,由于IS NULL和IS NOT NULL是互补的,因此不可能存在第三种情况.

In your original query, there is no possibility of a third case because IS NULL and IS NOT NULL are complementary, between them they have covered all possibilities.

此外,除非您使用区分大小写的排序规则(这种情况很少见,并且除非您特别指定一个,否则默认情况下从不进行排序),MySQL并非Oracle-这两个查询的工作方式相同:

Also, unless you are using case-sensitive collation (very rare, and never by default unless you specifically nominate one), MySQL is not Oracle - these two queries will work the same:

where name like '%Test%' or name like '%test%'
where name like '%test%'

因为MySQL将不区分大小写地匹配字符串

Because MySQL will match strings case-insensitively

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

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