MySQL 基本查询、条件查询、投影查询

文章目录

    • 1. 基本查询
    • 2. 条件查询
    • 3. 投影查询
    • 练习 LeetCode 595. 大的国家

学习自 廖雪峰的官方网站

1. 基本查询

SELECT * FROM <表名>

*表示所有内容
MySQL 基本查询、条件查询、投影查询_第1张图片

许多检测工具会执行一条SELECT 1; 来测试数据库连接。

2. 条件查询

SELECT * FROM <表名> WHERE <条件表达式>

条件运算按照NOT、AND、OR的优先级进行,即 NOT 最高,其次AND,最后OR
加括号 可以改变 优先级

SELECT * FROM students WHERE score >= 80;
SELECT * FROM students WHERE score >= 80 AND gender = 'M';
SELECT * FROM students WHERE score >= 80 OR gender = 'M';
SELECT * FROM students WHERE NOT class_id = 2;
SELECT * FROM students WHERE (score < 80 OR score > 90) AND gender = 'M';

3. 投影查询

结果集仅包含指定列

SELECT1,2,3 FROM <表名> WHERE <条件>
SELECT id, score, name FROM students;

# 下面的 score 改了名称为 points(重命名)
SELECT id, score points, name FROM students;

练习 LeetCode 595. 大的国家

题目:

Create table If Not Exists World (name varchar(255), continent varchar(255), area int, population int, gdp int)
Truncate table World
insert into World (name, continent, area, population, gdp) values ('Afghanistan', 'Asia', '652230', '25500100', '20343000000')
insert into World (name, continent, area, population, gdp) values ('Albania', 'Europe', '28748', '2831741', '12960000000')
insert into World (name, continent, area, population, gdp) values ('Algeria', 'Africa', '2381741', '37100000', '188681000000')
insert into World (name, continent, area, population, gdp) values ('Andorra', 'Europe', '468', '78115', '3712000000')
insert into World (name, continent, area, population, gdp) values ('Angola', 'Africa', '1246700', '20609294', '100990000000')

这里有张 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     |
+-----------------+------------+------------+--------------+---------------+

如果一个国家的面积超过300万平方公里,人口超过2500万,那么这个国家就是大国家

编写一个SQL查询,输出表中所有大国家名称、人口和面积

例如,根据上表,我们应该输出:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/big-countries
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


解题:

# Write your MySQL query statement below
SELECT name, population, area FROM World WHERE population > 25000000 OR area > 3000000;

格式无特殊要求,好像

# Write your MySQL query statement below
SELECT name, population, area 
FROM World 
WHERE population > 25000000 
OR area > 3000000;

217 ms

你可能感兴趣的:(MySQL)