
- 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 select * with distinct id?
You can use GROUP BY command for select with distinct id. The syntax is as follows −
SELECT *FROM yourTableName GROUP BY yourColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table DistinctIdDemo -> ( -> Id int, -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (1.03 sec)
Insert some records in the table using insert command. Here, we have added ID with duplicate values.
The query is as follows −
mysql> insert into DistinctIdDemo values(1,'Mike',23); Query OK, 1 row affected (0.16 sec) mysql> insert into DistinctIdDemo values(2,'Sam',24); Query OK, 1 row affected (0.20 sec) mysql> insert into DistinctIdDemo values(1,'Carol',23); Query OK, 1 row affected (0.15 sec) mysql> insert into DistinctIdDemo values(1,'John',28); Query OK, 1 row affected (0.33 sec) mysql> insert into DistinctIdDemo values(3,'David',26); Query OK, 1 row affected (0.22 sec) mysql> insert into DistinctIdDemo values(2,'Larry',29); Query OK, 1 row affected (0.20 sec)
Let us now display records −
mysql> select *from DistinctIdDemo;
The following is the output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 1 | Mike | 23 | | 2 | Sam | 24 | | 1 | Carol | 23 | | 1 | John | 28 | | 3 | David | 26 | | 2 | Larry | 29 | +------+-------+------+ 6 rows in set (0.00 sec)
Here is the query to get all records from the table with distinct id −
mysql> select *from DistinctIdDemo group by Id;
The following is the output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 1 | Mike | 23 | | 2 | Sam | 24 | | 3 | David | 26 | +------+-------+------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to select distinct order by id
- MySQL - SELECT … WHERE id IN (..) order with particular column?
- MySQL SELECT DISTINCT and count?
- MySQL query to group concat distinct by Id?
- SELECT DISTINCT vs GROUP BY in MySQL?
- MYSQL select DISTINCT values from two columns?
- MySQL Select Statement DISTINCT for Multiple Columns?
- How can I select the row with the highest ID in MySQL?
- How to select everything before @ in an email-id with in MySQL?
- Select distinct values from two columns in MySQL?
- How to speed up SELECT DISTINCT in MySQL
- Randomly SELECT distinct rows in a MySQL table?
- Select distinct combinations from two columns in MySQL?
- How to select ID column as null in MySQL?
- Select distinct values from three columns and display in a single column with MySQL

Advertisements