MySQL SQL 选择不同的行并在空白时忽略行

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

SQL select distinct rows and ignore row if blank

mysqlphpmyadmin

提问by user930026

I am using sql query to fetch rows from table. I want to select the rows only with distinct values and if there is no value entered for some row, that row should not be there.

我正在使用 sql 查询从表中获取行。我只想选择具有不同值的行,如果某行没有输入值,则该行不应该在那里。

SELECT DISTINCT meta_value FROM `wp_postmeta` WHERE meta_key = "aaa";

This is the query I am using, I am getting the distinct rows by this query but also getting the blank row.

这是我正在使用的查询,我通过此查询获取不同的行,但也获取空行。

回答by mattmanser

Simple solution:

简单的解决方案:

SELECT DISTINCT meta_value 
FROM `wp_postmeta` 
WHERE meta_key = "aaa" AND meta_value != "";

回答by Query Master

Try this query with IS NOT NULL

试试这个查询 IS NOT NULL

SELECT DISTINCT meta_value
FROM `wp_postmeta`
WHERE meta_key = "aaa"
AND meta_value IS NOT NULL ;

回答by ninjabber

I would use

我会用

where meta_key = "aaa"
AND (meta_value IS NOT NULL or meta_value != "");

but it's up to the engine you are using and most important - the way you are inserting empty values. NULL <> 0 <> ' '

但这取决于您使用的引擎,最重要的是 - 您插入空值的方式。NULL <> 0 <> ' '

回答by Shefali Aggarwal

SELECT DISTINCT meta_value
FROM `wp_postmeta`
WHERE meta_key = "aaa"
AND meta_value != "";

回答by Shefali Aggarwal

You need to handle null value in meta_valuecolumn. You can do like as follow.

您需要处理meta_value列中的空值。你可以做如下。

SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = 'aaa' and meta_value != ISNULL(meta_value,'')

hope this will work fine.

希望这会正常工作。

回答by Nitika Chopra

In this query, different values are shown, if a column contains some null values are also removed.

在此查询中,会显示不同的值,如果列包含一些空值也会被删除。

"select distinct color from tbl_product where color is not null"

select distinct color from tbl_product where color is not null

I hope this code is helpful for all..

我希望这段代码对大家有帮助..