Friday, February 24, 2012

Avoid getting same country twice

I have a DB containing trips around the world.
I want to create a menu with all the countries we visit but at the moment it returns the whole column. i.e. Brazil 5 times etc.
How do I avoid repititions?
I thought I could make an array and check it to see if the country was already there, but I think SQL must have a native routine to achieve the same thing.
Thanks
MFirst of all, you should check your data model and select statement you use to retrieve countries. Probably your SELECT is not defined properly if it returns multiple rows. Anyway, you can retrieve unique names using DISTINCT:

select DISTINCT CountryName
from ...|||cool, thanks er.. madafaka.
my SQL looks like this now, and works a treat:

"SELECT DISTINCT Country, url_link FROM dest_search ORDER BY country"

knowing me there's probably still a better way!
Incidentally the url_link is the path to the detail page and I gather doing it this way allows the search engines to follow the links. at first I put just:
"SELECT DISTINCT Country FROM dest_search ORDER BY country"
but that gave me an error because it excluded the url_link data.

Cheers
M

No comments:

Post a Comment