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 07:13:19)
Associated topics are indexed under
S157 - More MySQL Commands [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)
Some other Articles
Natural or man-made?Short weekendWhat they are saying about our OF COURSE newsletterSpotted - the local MPGetting a list of unique values from a MySQL columnCover all the optionsFire drillAn apology to Mr BoneparteSee 8 but buy 6. See 6 but you can buy 8.Our most popular resources