
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- 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 group by column and display the sum of similar values in another column
- MySQL query to display only the empty and NULL values together?
- Need help selecting non-empty column values from MySQL?
- Find and display duplicate values only once from a column in MySQL
- How to select only non - numeric values from varchar column in MySQL?
- MySQL query to display only the column values with corresponding column having whitespace
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING
- 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 record with maximum count values in a group with other column values?
- Find duplicate column values in MySQL and display them
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- 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?

Advertisements