大数据,机器学习,人工智能人工智能/模式识别/机器学习精华专题大数据 爬虫Python AI Sql

初识SQLzoo

2019-10-04  本文已影响0人  皮皮大

sqlzoo是一个专门用来练习SQL语句查询的网站,上面分成了各个板块或者语句,先是网站的例题,用户可以稍微修改提交,然后有各种由简到难的查询练习,很适合学习SQL语句的小伙伴进行入门。

sqlzoo

image.png

所用的表结构

image.png image.png

基本查询

-- 指定字段条件匹配
select population from world where name = 'Germany'  # 单个条件
select name, area, population from world where area > 5000 and population < 1000000;  # 多个条件查询

-- in匹配
-- 将需要匹配的条件放在列表中
select name, population from world where name in ('Sweden', 'Norway', 'Denmark');  # 国家的名字需要用单引号括起来,不能使用双引号

-- between...and...匹配
select name,area from world where area between 200000 and 250000

-- like模糊查询
-- 通配符%表示任意内容
select name from world where name like '%a' or name like '%l';
select name from world where name like '%United%'   # 查询名字中包含United

-- 查询中使用函数
-- 使用length()函数
select name, length(name) from world where length(name) = 5 and region = "Europe";

-- 与and、或or、异或xor查询
select name, population, area from world where area > 3000000 xor population > 250000000;  # 异或查询

-- round使用
select name, round(population / 1000000, 2), round(gdp/ 1000000000, 2) from world where continent = 'South America';

注意

SQL 查询语句中字符串不能使用双引号,要用单引号

MySQL中round()函数

ROUND(f,p) returns f rounded to p decimal places.

The number of decimal places may be negative, this will round to the nearest 10 (when p is -1) or 100 (when p is -2) or 1000 (when p is -3) etc..

ROUND(7253.86, 0)    ->  7254
ROUND(7253.86, 1)    ->  7253.9   # 四舍五入保留一位
ROUND(7253.86, -3)    ->  7000    # 直接取整
上一篇下一篇

猜你喜欢

热点阅读