初识SQLzoo

作者: 皮皮大 | 来源:发表于2019-10-04 12:54 被阅读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 查询语句中字符串不能使用双引号,要用单引号

    • 单引号

      image.png
    • 双引号


      image.png

    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..

    • 函数返回的是最接近f的整数

    • 如果有第二个参数p,表示通过四舍五入取到的小数位

      • p>0:表示取整的小数位
      • p<0:表示直接取整数
    ROUND(7253.86, 0)    ->  7254
    ROUND(7253.86, 1)    ->  7253.9   # 四舍五入保留一位
    ROUND(7253.86, -3)    ->  7000    # 直接取整
    

    相关文章

      网友评论

        本文标题:初识SQLzoo

        本文链接:https://www.haomeiwen.com/subject/untspctx.html