MySQL DISTINCT usage


Home - Tutorials - MySQL

How to use the distinct keyword in MySQL to eliminate duplicated values in the result list of a select statement.

Tutorial info:


Name:MySQL DISTINCT usage
Total steps:1
Category:MySQL
Date:2010-11-10
Level:Beginner
Product:See complete product
Viewed:6166

Bookmark MySQL DISTINCT usage



AddThis Social Bookmark Button

Step 1 - MySQL DISTINCT explained


MySQL DISTINCT usage

In cases you want to eliminate duplicate values in the result list of a SELECT statement you can use the DISTINCT keyword. Simple put the DISTINCT before the column name.

The syntax is:

Code:
  1. SELECT DISTINCT column_name FROM table_name;

Let’s see a simple example where we have a list of cars. Every car has an id, brand, type and color but now we just simply list all brands:

Code:
  1. SELECT brand FROM car;
  2.  
  3. The result is:
  4. Audi                                                                                                                                                  
  5. Audi                                                                                                                                                  
  6. Audi                                                                                                                                                  
  7. BMW                                                                                                                                                   
  8. BMW                                                                                                                                                   
  9. Lexus                                                                                                                                                 
  10. Lexus                                                                                                                                                 

As you can see the brands displayed several times. However what we want is to know how many different brands are in the db. To get this extend the query as follows:

Code:
  1. SELECT DISTINCT brand FROM car;
  2.  
  3. The result is:
  4. Audi                                                                                                                                                  
  5. BMW                                                                                                                                                   
  6. Lexus                                                                                                                                                 

What if if you also want to know how many times the different brands occur in the table. To do this we need to use a bit different approach using COUNT() and GROUP BY as you can see in this example:

Code:
  1. SELECT brand, COUNT(brand) FROM car GROUP BY brand;
  2.  
  3. And the result is:
  4. brand		count(brand)         
  5. Audi		3                    
  6. BMW		2                    
  7. Lexus		2

 






Tags: mysql distinct, remove duplicates, group by, unique values, distinct values

MySQL DISTINCT usage - Table of contents
Step 1 - MySQL DISTINCT explained



F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

Family tutorials
PHP Array
PHP Redirect
PHP Session
JavaScript String
JavaScript Timeout
JavaScript Tooltip
AJAX PHP
AJAX File Upload
AJAX Rating System

Total time: 0.0305