
- 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 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 Articles
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- How to SELECT all values from a table only once if they're duplicated?
- MySQL select only duplicate records from database and display the count as well?
- MySQL query to replace only the NULL values from the table?
- 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
- Select query to display duplicate values with max date
- Find and display duplicate values only once from a column in MySQL
- MySQL query to display only the empty and NULL values together?
- How to select distinct value from one MySQL column only?
- MySQL query to compare and display only the rows with NULL values
- How to remove only the first word from columns values with a MySQL query?
- MySQL query to return the count of only NO values from corresponding column value
- MySQL select only a single value from 5 similar values?
- MySQL query to group results by date and display the count of duplicate values?

Advertisements