
- 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 DISTINCT and count?
You need to use GROUP BY command with aggregate function count(*) from MySQL to achieve this. The syntax is as follows:
SELECT yourColumnName,COUNT(*) AS anyVariableNameFROM 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 selectDistinct_CountDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(10), -> AppearanceId int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into selectDistinct_CountDemo(Name,AppearanceId) values('Larry',1); Query OK, 1 row affected (0.18 sec) mysql> insert into selectDistinct_CountDemo(Name,AppearanceId) values('John',2); Query OK, 1 row affected (0.24 sec) mysql> insert into selectDistinct_CountDemo(Name,AppearanceId) values('Larry',3); Query OK, 1 row affected (0.12 sec) mysql> insert into selectDistinct_CountDemo(Name,AppearanceId) values('Larry',10); Query OK, 1 row affected (0.18 sec) mysql> insert into selectDistinct_CountDemo(Name,AppearanceId) values('Carol',11); Query OK, 1 row affected (0.18 sec) mysql> insert into selectDistinct_CountDemo(Name,AppearanceId) values('Larry',15); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from selectDistinct_CountDemo;
The following is the output:
+----+-------+--------------+ | Id | Name | AppearanceId | +----+-------+--------------+ | 1 | Larry | 1 | | 2 | John | 2 | | 3 | Larry | 3 | | 4 | Larry | 10 | | 5 | Carol | 11 | | 6 | Larry | 15 | +----+-------+--------------+ 6 rows in set (0.00 sec)
Here is the query to select distinct and count:
mysql> select Name,count(*) as TotalAppearance from selectDistinct_CountDemo -> group by Name;
The following is the output:
+-------+-----------------+ | Name | TotalAppearance | +-------+-----------------+ | Larry | 4 | | John | 1 | | Carol | 1 | +-------+-----------------+ 3 rows in set (0.00 sec)
- Related Articles
- MongoDB query to select distinct and count?
- MySQL select * with distinct id?
- Get distinct values and count them in MySQL
- Can I use MySQL COUNT() and DISTINCT together?
- SELECT DISTINCT vs GROUP BY in MySQL?
- MYSQL select DISTINCT values from two columns?
- MySQL Select Statement DISTINCT for Multiple Columns?
- Using DISTINCT and COUNT together in a MySQL Query?
- MySQL select count by value?
- How to count distinct values 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?
- MySQL query to select distinct order by id

Advertisements