Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Getting a list of unique values from a MySQL column

Would you like to get a list of all the different values in a column? Use the DISTINCT keyword. Here's an example:


mysql> select * from train;
+-------+--------+-------------+-----+
| time | length | destination | tid |
+-------+--------+-------------+-----+
| 07:05 | 1 | Salisbury | 1 |
| 08:18 | 2 | Swindon | 2 |
| 09:05 | 2 | Southampton | 3 |
| 05:45 | 1 | Swindon | 4 |
| 10:35 | 12 | Plymouth | 10 |
| 13:49 | 2 | Swindon | 6 |
| 14:20 | 2 | Salisbury | 7 |
| 17:07 | 1 | Swindon | 8 |
| 18:18 | 1 | Salisbury | 9 |
| 13:00 | 6 | Windsor | 11 |
+-------+--------+-------------+-----+
10 rows in set (0.04 sec)

mysql> select distinct destination from train;
+-------------+
| destination |
+-------------+
| Salisbury |
| Swindon |
| Southampton |
| Plymouth |
| Windsor |
+-------------+
5 rows in set (0.08 sec)

mysql>


See More on SQL module.


(written 2005-04-14, updated 2006-06-05)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
S157 - More MySQL commands
  [3270] SQL - Data v Metadata, and the various stages of data selection - (2011-04-29)
  [3061] Databases - why data is split into separate tables, and how to join them - (2010-11-20)
  [2647] Removing duplicates from a MySQL table - (2010-02-22)
  [2645] Optimising and caching your MySQL enquiries - (2010-02-22)
  [2644] Counting rows in joined MySQL tables - (2010-02-22)
  [2643] Relating tables with joins in MySQL - (2010-02-21)
  [2448] MySQL - efficiency and other topics - (2009-10-10)
  [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
  [2110] MySQL - looking for records in one table that do NOT correspond to records in another table - (2009-03-31)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
  [1574] Joining MySQL tables revisited - finding nonmatching records, etc - (2008-03-15)
  [1331] MySQL joins revisited - (2007-09-03)
  [1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
  [1213] MySQL - the order of clauses and the order of actions - (2007-06-01)
  [673] Helicopter views and tartans - (2006-04-06)
  [591] Key facts - SQL and MySQL - (2006-02-04)
  [581] Saving a MySQL query results to your local disc for Excel - (2006-01-29)
  [572] Giving the researcher power over database analysis - (2006-01-22)
  [567] Combining similar rows from a MySQL database select - (2006-01-17)
  [517] An occasional chance, and reducing data to manageable levels - (2005-12-04)
  [515] MySQL - an FAQ - (2005-12-03)
  [513] MySQL - JOIN or WHERE to link tables correctly? - (2005-12-01)
  [502] SELECT in MySQL - choosing the rows you want - (2005-11-22)
  [494] MySQL - a score of things to remember - (2005-11-12)
  [449] Matching in MySQL - (2005-09-24)
  [159] MySQL - Optimising Selects - (2004-12-21)
  [158] MySQL - LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN - (2004-12-20)


Back to
Cover all the options
Previous and next
or
Horse's mouth home
Forward to
Spotted - the local MP
Some other Articles
Natural or man-made?
Short weekend
What they are saying about our OF COURSE newsletter
Spotted - the local MP
Getting a list of unique values from a MySQL column
Cover all the options
Fire drill
An apology to Mr Boneparte
See 8 but buy 6. See 6 but you can buy 8.
Our most popular resources
4316 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

Comment: "Havent seen your site crop up in sql searches for years. I ..."
Visitor Ranking 4.0 (5=excellent, 1=poor)

Comment by Colin (published 2010-10-08) Suggested link.
Havent seen your site crop up in sql searches for years. I this page found searching for a solution to a slightly different problem:
I need a list of unique values AND an id. Any id will do. First or last.
So how from your table: How to return the distinct destination along with a `tid` Pick a tid, any tid?

stackoverflow has a similar prob. I give link. My col I want unique has some heavy calcs so am trying to avoid multi selects and joins as far as poss. [#3783]

You can Add a comment or ranking or edit your own comments

Average page ranking - 4.0

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/279_Gett ... olumn.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat