标签:count should span leetcode select 查询条件 ica big 代码
There is a table World
+-----------------+------------+------------+--------------+---------------+ | name | continent | area | population | gdp | +-----------------+------------+------------+--------------+---------------+ | Afghanistan | Asia | 652230 | 25500100 | 20343000 | | Albania | Europe | 28748 | 2831741 | 12960000 | | Algeria | Africa | 2381741 | 37100000 | 188681000 | | Andorra | Europe | 468 | 78115 | 3712000 | | Angola | Africa | 1246700 | 20609294 | 100990000 | +-----------------+------------+------------+--------------+---------------+
A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.
Write a SQL solution to output big countries‘ name, population and area.
For example, according to the above table, we should output:
+--------------+-------------+--------------+ | name | population | area | +--------------+-------------+--------------+ | Afghanistan | 25500100 | 652230 | | Algeria | 37100000 | 2381741 | +--------------+-------------+--------------+
题意:找到表中人口大于2500 0000或者面积大于300 0000 平方公里的国家。
解题思路:
数据库的题解题思路就比较单一了,首先知道要什么之后设定查询条件,这里有两个那么就是where 条件一 or 条件二,观察结果,表头是name,population,
area, 那么select World.name World.population World.area,没要求按照什么顺序排序所以就可以忽略排序部分,所以连起来代码如下:
1 SELECT name,population,area 2 FROM World 3 WHERE World.population>25000000 4 OR World.area>3000000;
当然也可以使用union方式,代码要稍微多一点,理论上运行会比where略快。union相当于使用两个索引同时进行查找,而or相当于从一个索引查完之后去另一个索引查。理论上是这样但是你实际运行的时候,oj对你的响应不一定那么快,所以两个可能不一定谁快谁慢
1 SELECT name, population, area 2 FROM World 3 WHERE area > 3000000 4 UNION 5 SELECT name, population, area 6 FROM World 7 WHERE population > 25000000
简单的问题,可以用来对数据库查询进行初步的掌握。
LeetCode解题思路:595. Big Countries
标签:count should span leetcode select 查询条件 ica big 代码
原文地址:http://www.cnblogs.com/hellomotty/p/7327910.html