如何在 MySQL 中选择非空列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14575807/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:20:34  来源:igfitidea点击:

How to select non-empty columns in MySQL?

mysql

提问by Nate

Sorry if this has been asked before, but I've been searching and can't find a solution to my simple problem.

抱歉,如果之前有人问过这个问题,但我一直在搜索,但找不到解决我的简单问题的方法。

I need to select all rows where a varchar column in it has text in it (letters, numbers, or special characters), so I wrote a query like the following:

我需要选择 varchar 列中包含文本(字母、数字或特殊字符)的所有行,因此我编写了如下查询:

SELECT col1
FROM table
WHERE col1 IS NOT NULL

But this returns columns that contain the value '' (i.e. it's blank, but is not set to NULL so it still gets returned).

但这会返回包含值 '' 的列(即它是空白的,但未设置为 NULL,因此它仍然会被返回)。

I tried changing the query to this:

我尝试将查询更改为:

SELECT col1
FROM table
WHERE col1 IS NOT NULL
AND col1 != ''

But that didn't work.

但这没有用。

How do I stop the rows where the column is blank from being returned?

如何阻止返回列为空白的行?

回答by Raab

use TRIMthere seems to be spaces in your column then

使用TRIM您的列中似乎有空格然后

SELECT col1
FROM table
WHERE col1 IS NOT NULL
AND TRIM(col1) <> ''

回答by Shankar Kalyankar

You can use this query if column is blank, but is not set to NULL

如果列为空但未设置为 NULL,则可以使用此查询

SELECT col1 FROM table where col1 not like '';# Two Single quotes without space

SELECT col1 FROM table where col1 not like '';# 两个没有空格的单引号

OR

或者

This if column has one or more spaces,

这如果列有一个或多个空格,

SELECT col1 FROM table where col1 not like '% %';

回答by sj59

use LENGTH:

使用LENGTH

SELECT col1
FROM table
WHERE LENGTH(col1) > 0