SELECT from SQL
I have a tab开发者_如何学Gole which contains the names of a country, the population of the country and the countries GDP, how would I display the names of the country and their per capita GDP
SELECT name, gdp/NullIf(population,0) AS PerCapitaGDP FROM "MyCountryTable"
SQL allows calculations to be performed inline, like:
SELECT Name, GDP / Population AS [Per Capita GDP] FROM YourTable
To deal with the 0 population division by zero, I'd suggest using a CASE statement
SELECT
Name,
CASE IsNull(population,0)
WHEN 0
THEN 0
ELSE
THEN gdp/population
END AS 'PerCapitaGDP'
FROM Countries
This will work on MS SQL Server, you may need to look up syntax for which DBMS you are using (for example, it looks like MySQL uses END CASE instead of just END)
精彩评论