
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
GROUP BY and display only non-empty column values in MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Id varchar(100), -> Message varchar(200) -> ); Query OK, 0 rows affected (1.17 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('1',''); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('1','Hi'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('2','Hello'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('3','Awesome'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('3','Good Morning'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2',NULL); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------+--------------+ | Id | Message | +------+--------------+ | 1 | | | 1 | Hi | | 2 | Hello | | 3 | Awesome | | 3 | Good Morning | | 2 | NULL | +------+--------------+ 6 rows in set (0.00 sec)
Following is the query to group by and select first non-empty columns −
mysql> select Id,min(Message) from DemoTable -> where Message IS NOT NULL and length(Message) > 0 -> group by Id;
Output
+------+--------------+ | Id | min(Message) | +------+--------------+ | 1 | Hi | | 2 | Hello | | 3 | Awesome | +------+--------------+ 3 rows in set (0.06 sec)
- Related Questions & Answers
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- MySQL query to display only the empty and NULL values together?
- Need help selecting non-empty column values from MySQL?
- MySQL query to group by column and display the sum of similar values in another column
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING
- Find and display duplicate values only once from a column in MySQL
- How to select only non - numeric values from varchar column in MySQL?
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL query to group by names and display the count in a new column
- MySQL query to display only the column values with corresponding column having whitespace
- Looping in JavaScript to count non-null and non-empty values
- Query non-empty values of a row first in ascending order and then display NULL values
- MySQL query to group results by date and display the count of duplicate values?
- Python – Display only non-duplicate values from a DataFrame
- MySQL query to select rows where column value is only 0, group by another column?
Advertisements