
- 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
MySQL query to skip the duplicate and select only one from the duplicated values
The syntax is as follows to skip the duplicate value and select only one from the duplicated values −
select min(yourColumnName1),yourColumnName2 from yourTableName group by yourColumnName2;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table doNotSelectDuplicateValuesDemo -> ( -> User_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> User_Name varchar(20) -> ); Query OK, 0 rows affected (0.78 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Carol'); Query OK, 1 row affected (0.09 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Carol'); Query OK, 1 row affected (0.08 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Sam'); Query OK, 1 row affected (0.28 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Bob'); Query OK, 1 row affected (0.16 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('David'); Query OK, 1 row affected (0.21 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Maxwell'); Query OK, 1 row affected (0.13 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Ramit'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from doNotSelectDuplicateValuesDemo;
Here is the output −
+---------+-----------+ | User_Id | User_Name | +---------+-----------+ | 1 | John | | 2 | Carol | | 3 | Carol | | 4 | Carol | | 5 | Sam | | 6 | Mike | | 7 | Bob | | 8 | David | | 9 | Maxwell | | 10 | Bob | | 11 | Ramit | +---------+-----------+ 11 rows in set (0.00 sec)
Here is the query to skip the duplicate value and select only one from the duplicated values −
mysql> select min(User_Id),User_Name from doNotSelectDuplicateValuesDemo group by User_Name;
Here is the output −
+--------------+-----------+ | min(User_Id) | User_Name | +--------------+-----------+ | 1 | John | | 2 | Carol | | 5 | Sam | | 6 | Mike | | 7 | Bob | | 8 | David | | 9 | Maxwell | | 11 | Ramit | +--------------+-----------+ 8 rows in set (0.07 sec)
- Related Questions & Answers
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- MySQL select only duplicate records from database and display the count as well?
- MySQL query to replace only the NULL values from the table?
- How to SELECT all values from a table only once if they're duplicated?
- MySQL query to fetch the maximum corresponding value from duplicate column values
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- How to select distinct value from one MySQL column only?
- MySQL query to display only the empty and NULL values together?
- Find and display duplicate values only once from a column in MySQL
- Select query to display duplicate values with max date
- MongoDB query select and display only a specific field from the document?
- MySQL query to increment one of the column values
- MySQL query to compare and display only the rows with NULL values
- MySQL query to get only the minutes from datetime?
- MySQL query to sort column values and ignoring quotes on one of the values
Advertisements