The clause 'admin1=00' is not required.
For the alternatenames you should join the two tables (geoname and alternatename table) on the geonameid. If you only are interested in Spanish you can filter for isolanguage='es'.
have 192 country (I have deteted country whit no population)
If I use this
Code:
SELECT *
FROM geoname, alternatename
WHERE geoname.fcode = 'PCLI'
AND geoname.geonameid = alternatename.geonameid
AND alternatename.isoLanguage = 'en' AND alternatename.isShortName=1
have 117 country
if query is
Code:
SELECT *
FROM geoname, alternatename
WHERE geoname.fcode = 'PCLI'
AND geoname.geonameid = alternatename.geonameid
AND alternatename.isoLanguage = '".$lang."' AND (alternatename.isShortName=1 OR alternatename.isPreferredName=1)
of course. not every country has an entry for each language in the alternatenatename table, and not every country with an entry has the flags set, and others have both flags set.
You can either do a preprocessing when loading the geonames data or you have to use more complex queries with outer joins etc.
I would do it as a preprocessing on the application level otherwise if you want to do it on the db you can use window functions if your database supports it. Sort the window by your favorite criteria and get the best one with the first_value function.
For postgres it would look like this:
Code:
select distinct countrycode,first_value(name) over (partition by countrycode) as bestname from (select countrycode,case when alternatename.name is not null then alternatename.name else geoname.name end from geoname left outer join alternatename on alternatename.geonameid=geoname.geonameid and alternatename.namelocale='en' where featureclass ='A' and featurecode like 'PCL%' order by countrycode,isShortName desc nulls last,isofficialname desc nulls last)t order by countrycode;