1. Show the name - but substitute Australasia for Oceania - for countries beginning with N.
显示以N开头的国家并且用Australasia代替Oceania
答案:SELECT name, CASE WHEN continent='Oceania' THEN 'Australasia ELSE continent END FROM world WHERE name LIKE 'N%';
2. Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America- for each country in North America or South America or Caribbean. Show countries beginning with A or B
答案:SELECT name,CASE WHEN continent IN ('Europe','Asia') THEN 'Eurasia'
WHEN continent IN('North America','South America','Caribbean') THEN 'America'
ELSE continent
END
FROM world
WHERE name LIKE 'A%' OR name LIKE 'B%'
用到的关键语法,替换:CASE WHEN ... THEN ... WHEN... THEN... ELSE... END
3. Put the continents right...
Oceania becomes Australasia
Countries in Eurasia and Turkey go to Europe/Asia
Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America
Show the name, the original continent and the new continent of all countries.
答案:
SELECT name,continent,
CASE
WHEN continent = 'Oceania' THEN 'Australasia'
WHEN continent IN ('Eurasia','Turkey') THEN 'Europe/Asia'
WHEN continent = 'Caribbean'THEN
CASE
WHEN name LIKE 'B%' THEN 'North America'
ELSE 'South America'
END
ELSE continent
END
FROM world
ORDER BY name