索引 对数 什么 条件 solution style etc area
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,没要求按照什么顺序排序所以就可以忽略排序部分,所以连起来代码如下:
- SELECT name,population,area
- FROM World
- WHERE World.population>25000000
- OR World.area>3000000;
当然也可以使用 union 方式,代码要稍微多一点,理论上运行会比 where 略快。union 相当于使用两个索引同时进行查找,而 or 相当于从一个索引查完之后去另一个索引查。理论上是这样但是你实际运行的时候,oj 对你的响应不一定那么快,所以两个可能不一定谁快谁慢
- SELECT name, population, area
- FROM World
- WHERE area > 3000000
- UNION
- SELECT name, population, area
- FROM World
- WHERE population > 25000000
简单的问题,可以用来对数据库查询进行初步的掌握。
LeetCode 解题思路:595. Big Countries
来源: http://www.bubuko.com/infodetail-2253174.html