
- 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
Delete rows with duplicate and similar content & get row with maximum number with MySQL select statement?
Let us first create a table −
mysql> create table DemoTable1468 -> ( -> Id int, -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (1.21 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1468 values(100,'Chris',23); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1468 values(101,'Bob',25); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1468 values(102,'David',30); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1468 values(100,'Chris',23); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable1468 values(100,'Chris',38); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1468 values(101,'Bob',23); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1468;
This will produce the following output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 23 | | 101 | Bob | 25 | | 102 | David | 30 | | 100 | Chris | 23 | | 100 | Chris | 38 | | 101 | Bob | 23 | +------+-------+------+ 6 rows in set (0.00 sec)
Here is the query to delete rows with duplicate and similar content and get row with max number with select statement −
mysql> select Id,Name,Age from DemoTable1468 -> group by Name,Id -> having max(Age);
This will produce the following output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 23 | | 101 | Bob | 25 | | 102 | David | 30 | +------+-------+------+ 3 rows in set (0.00 sec)
- Related Articles
- Return only a single row from duplicate rows with MySQL
- Get maximum age from records with similar student names in MySQL
- MySQL SELECT IF statement with OR?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Select specific rows in a range with MySQL?
- Delete only specific rows in a table with MySQL
- Get the number of rows in a particular table with MySQL
- Get rows with GROUP BY in MySQL?
- Concatenate multiple rows and columns in a single row with MySQL
- Get values from all rows and display it a single row separated by comma with MySQL
- Get timestamp date range with MySQL Select?
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
- MySQL: Insert a row and get the content?
- How to select rows with condition via concatenate in MySQL?
- Delete one row and reorder the others with the correct ID in MySQL?

Advertisements