我在一个表中有一个列,它可能包含空值或空值。我如何检查一个列是否为空或空的行中存在的表?

(e.g. null or '' or '  ' or '      ' and ...)

当前回答

select * from table where length(RTRIM(LTRIM(column_name))) > 0

其他回答

要么

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

or

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

写条件的一种更简短的方法:

WHERE some_col > ''

因为null > "会产生unknown,所以它会过滤掉空字符串和空字符串。

SELECT * FROM tbl WHERE trim(IFNULL(col,'')) <> '';

下面的SQL查询工作正常。

SELECT * FROM <table-name> WHERE <column-name> IS NULL;

我的意见。

在MySQL中,你可以使用COALESCE函数:

返回列表中的第一个非NULL值,如果没有非NULL值,则返回NULL。

所以你可以这样简化你的查询:

SELECT * FROM table WHERE COALESCE(some_col, '') = '';