
- 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
MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
For this, use aggregate function MAX() along with the GROUP BY clause. Let us first create a table −
mysql> create table DemoTable ( Id int, Value1 int, Value2 int, Value3 int, Value4 int ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Id,Value4) values(100,30); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Id,Value1,Value2,Value3) values(100,20,60,40); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Id,Value2,Value3,Value4) values(100,90,100,110); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+--------+--------+--------+--------+ | Id | Value1 | Value2 | Value3 | Value4 | +------+--------+--------+--------+--------+ | 100 | NULL | NULL | NULL | 30 | | 100 | 20 | 60 | 40 | NULL | | 100 | NULL | 90 | 100 | 110 | +------+--------+--------+--------+--------+ 3 rows in set (0.00 sec)
Following is the query to merge rows if Id is the same and display the highest corresponding value from other columns −
mysql> select Id,max(Value1) as Value1,max(Value2) as Value2,max(Value3) as Value3,max(Value4) as Value4 from DemoTable group by Id;
This will produce the following output −
+------+--------+--------+--------+--------+ | Id | Value1 | Value2 | Value3 | Value4 | +------+--------+--------+--------+--------+ | 100 | 20 | 90 | 100 | 110 | +------+--------+--------+--------+--------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to sum rows having repeated corresponding Id
- MySQL query to find the average of rows with the same ID
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value
- Display highest amount from corresponding duplicate ids in MySQL
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- MySQL UPDATE query where id is highest AND field is equal to variable?
- Get rows that have common value from the same table with different id in MySQL
- How to order by the highest value from two columns in MySQL?
- Only display row with highest ID in MySQL
- MySQL - SUM rows with same ID?
- MySQL query to fetch the maximum corresponding value from duplicate column values
- MySQL Select Rows where two columns do not have the same value?
- Get the highest score value from a single column and the greatest from two columns in MySQL

Advertisements