需要帮助从 MySQL 中选择非空列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6947217/
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
Need help selecting non-empty column values from MySQL
提问by codacopia
I have a MySQL table which has about 30 columns. One column has empty values for the majority of the table. How can I use a MySQL command to filter out the items which do have values in the table?
我有一个大约有 30 列的 MySQL 表。对于表的大部分,一列具有空值。如何使用 MySQL 命令过滤掉表中具有值的项目?
Here is my attempt:
这是我的尝试:
SELECT * FROM `table` WHERE column IS NOT NULL
This does not filter because I have empty cells rather that having NULLin the void cell.
这不会过滤,因为我有空单元格而不是NULL在空单元格中。
回答by Michael Berkowski
Also look for the columns not equal to the empty string ''
还要查找不等于空字符串的列 ''
SELECT * FROM `table` WHERE column IS NOT NULL AND column <> ''
If you have fields containing only whitespace which you consider empty, use TRIM()to eliminate the whitespace, and potentially leave the empty string ''
如果您的字段只包含您认为为空的空格,请使用TRIM()消除空格,并可能保留空字符串''
SELECT * FROM `table` WHERE column IS NOT NULL AND TRIM(column) <> ''
回答by user1368905
A alternate approach that also handles blank spaces in a column as well as null:
还处理列中的空格以及空值的另一种方法:
SELECT * FROM `table` WHERE TRIM(column) > ''

