ErickBest
ErickBest

Reputation: 4692

MySQL SELECT FROM TABLE

I have got a TABLE like:

 id | names | fav_color
|------------------------
|1  | John  | black
|2  | Isaac | orange
|3  | Ruth  | blue
|4  | Ezra  | black
|5  | Luke  | orange
|6  | Acts  | blue
|7  | Apoca | black
|8  | James | orange
|9  | Roma  | yellow

I want a Query that will SELECT all the fav_colors but return them distinctively with no duplicates.

Something like

fav_color
---------
black
orange
blue
yellow

Upvotes: 1

Views: 65

Answers (2)

Erik Schierboom
Erik Schierboom

Reputation: 16636

Mahmoud has given you the correct answer. Should you want to know the number of rows for each fav_color you could do:

SELECT fav_color, COUNT(*) AS count FROM tablename GROUP BY fav_color;

This will output:

fav_color|count
--------- -----
black     3 
orange    3
blue      2
yellow    1

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79909

Use DISTINCT:

SELECT DISTINCT fav_color FROM tablename;

See it in action here:

This will give you something like:

| FAV_COLOR |
-------------
|     black |
|    orange |
|      blue |
|    yellow |

Upvotes: 4

Related Questions