Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select distinct rows and ignore row if blank

Tags:

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.

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.

like image 834
user930026 Avatar asked Jun 06 '12 10:06

user930026


People also ask

How do I ignore blank values in SQL?

SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Query: SELECT * FROM Student WHERE Name IS NOT NULL AND Department IS NOT NULL AND Roll_No IS NOT NULL; To exclude the null values from all the columns we used AND operator.

How do I exclude a row in SQL query?

Use the relational operators != or <> to exclude rows in a WHERE clause. The following query assumes that you are selecting from an ANSI-compliant database; the statements specify the owner or login name of the creator of the customer table.

Does distinct ignore NULL?

In SQL, the DISTINCT clause doesn't ignore NULL values. So when using the DISTINCT clause in your SQL statement, your result set will include NULL as a distinct value.


1 Answers

Simple solution:

SELECT DISTINCT meta_value  FROM `wp_postmeta`  WHERE meta_key = "aaa" AND meta_value != ""; 
like image 62
mattmanser Avatar answered Oct 08 '22 05:10

mattmanser