This is a discussion on Distinct Syntax within the MySQL forums, part of the Database Server Software category; --> I am having trouble with how to use "distinct" I have a Table called "places" with the values "town" ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I am having trouble with how to use "distinct" I have a Table called "places" with the values "town" and "posnum" The data can look like this london, 333 london, 333 paris, 442 barcelona, 222 barcelona, 222 madrid, 222 madrid, 222 malaga, 988 malaga, 988 malaga, 988 malaga, 988 malaga, 988 I need to get a list of all the different towns and the value in "posnum". The value of "posnum" can apply to more than one town. But a town can only have one value for "posnum" What I need is to get the following list london, 333 paris, 442 barcelona, 222 madrid, 222 malaga, 988 If I use "SELECT DISTINCT town FROM places" I get a distinct list of the five towns, but I can not see the "posnum" field What I would like to do is select * from places and ignore duplicate towns. This way I can obtain the name of each town once along with its "posnum" However, I can't work out the syntax to do this. Any help greatfully appreciated. Garry Jones Sweden |
| ||||
| > What I need is to get the following list > > london, 333 > paris, 442 > barcelona, 222 > madrid, 222 > malaga, 988 > > If I use > > "SELECT DISTINCT town FROM places" > > I get a distinct list of the five towns, but I can not see the > "posnum" field that is because you did not ask for it. SELECT DISTINCT town, posnum FROM places would give you that: the entire list, but with duplicate rows removed. If you want only one column to be distinct, read the manual about the GROUP BY clause. |