SQL之ROUND、CASE、LIKE语句

SQL当中的round函数可以对数值的小数位进行处理,比如保留两位小数如下所示:

select name,ROUND(population/1000000,2)as population, ROUND(gdp/1000000000,2) as gdp
from world
where continent='South America'

还可以用来除去小数字部分,比如2134变为2000,如下所示:

select name,round(gdp/population,-3)as gdp
from world
where gdp > 1000000000000

Case的使用
如下是将continent是Oceania的替换成Australasia,其他不变:

SELECT name, 
       CASE WHEN continent='Oceania' THEN 'Australasia'
            ELSE continent END
  FROM world
 WHERE name LIKE 'N%'

如下是多个CASE使用的情况

select name,
     CASE WHEN continent='Europe' or continent='Asia' THEN'Eurasia'
          when continent='South America' or continent='North America' or continent='Caribbean' then 'America'
          else continent end
from world
where name like 'A%' or name like 'B%'

使用了CASE同时还有ORDER By 以及like:

select name,continent,
CASE when continent='Oceania' then 'Australasia'
     when continent='Eurasia' or name = 'Turkey' then 'Europe/Asia'
     when continent='Caribbean' and name like 'B%' then 'North America'
     when continent='Caribbean' and name not like 'B%' then 'South America'
     ELSE continent end
from world

order by name ASC


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值