背景

目前复习sql语言,在前几篇博客详细描述了sql的语法,但是在刷sqlzoo过程中遇到一些没有学习的知识点,在这篇博客进行总结。

之前的复习笔记,可以作为参考:https://blog.csdn.net/Foools/article/details/109248894


文章目录

  • 背景
  • 一、round函数
  • 二、left函数
  • 三、查询内容中存在 '
  • 四、排序中,先对一些数据排序,再对另外的数据排序
  • 五、concat函数
  • 六、各个州的最大面积的国家
  • 七、group by和order by
  • 八、coalesce
  • 九、group by 1
  • 十、case when
  • 总结

一、round函数

ROUND 函数用于把数值字段舍入为指定的小数位数。decimals可以设置为负数。

SQL ROUND() 语法
SELECT ROUND(column_name,decimals) FROM table_name

例题
Show the name and population in millions and the GDP in billions for the countries of the continent ‘South America’. Use the ROUND function to show the values to two decimal places.
For South America show population in millions and GDP in billions both to 2 decimal places.

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

二、left函数

You can use the function LEFT to isolate the first character.本质上是一个截断操作,string类型的数据,进行从左端开始截取。

例题
Show the name and the capital where the first letters of each match. Don’t include countries where the name and the capital are the same word.

SELECT name,  capital
FROM world
where left(capital,1) = left(name,1) and name!= capital

三、查询内容中存在 ’

需要将’前面加一个/,就可以解决了,否则会出现很多’,导致查询出错

例题
Find all details of the prize won by EUGENE O’NEILL

select * from nobel where winner = 'EUGENE O\'NEILL'

四、排序中,先对一些数据排序,再对另外的数据排序

The expression subject IN (‘Chemistry’,‘Physics’) can be used as a value - it will be 0 or 1.在order by后面使用,即可实现先排序其他的,再排序chemistry,physics。

例题
Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

SELECT winner, subject FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject,winner

查询结果如下:

五、concat函数

concat函数用于连接单元,用于将两个字符串连接起来,形成一个单一的字符串。试试下面的例子:

例题:Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

select name,concat(round(population/(select population from world where name = 'Germany')*100,0),'%')from world where continent = 'Europe'

六、各个州的最大面积的国家

例题:Find the largest country (by area) in each continent, show the continent, the name and the area:

SELECT continent, name, area FROM world xWHERE area >= ALL(SELECT area FROM world yWHERE y.continent=x.continentAND area>0)

七、group by和order by

这俩的区别在于,group by是分组查询,而order by是排序查询,记清这一点,就不会搞混了。

例题List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

select f.name from (select movieid from casting c
join actor a on a.id=c.actorid where name='Art Garfunkel') as e
join (select movieid, name from casting c
join actor a on a.id=c.actorid where name!='Art Garfunkel') as f
on e.movieid=f.movieid

八、coalesce

用于填补空值,格式如下。

SELECT name, party,COALESCE(party,'None') AS affFROM msp WHERE name LIKE 'C%'

九、group by 1

这个子句的意思是,group by后面的1指代的是select后面的第一个。

十、case when

基本语句,一看就会:

CASE WHEN SCORE = 'A' THEN '优'WHEN SCORE = 'B' THEN '良'WHEN SCORE = 'C' THEN '中' ELSE '不及格' END

总结

之后刷到别的sql有意思的知识点也会在这里更新。

SQLZOO知识点补充相关推荐

  1. 计算机网络知识点补充

    计算机网络知识点补充 目录: 1. HTTP和HTTPS的区别 http协议是运行在tcp之上,明文传输,客户端和服务器端都无法验证对方的身份:https是运行在ssl(Secure Socket L ...

  2. set集合以及知识点补充

    str知识点补充: #join() 将列表中的每个元素中间加上一个字符,连接成一个字符串 list = ['a','b','c','d'] str = '*'.join(list) print(str ...

  3. Jaeger知识点补充

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos <Jaeger实战(Java版)>系列全文 ...

  4. 云日记_登出操作个人中心页面js和jsp知识点补充

    用户退出     前端:         设置超链接的请求地址  user?actionName=logout     后台:         1. 销毁Session对象         2. 删除 ...

  5. Python-day06-2018.7.9_编码以及小知识点补充

    本节知识点: 1.is 和 == 的区别 2.编码的问题 一.is 和 == 的区别 1.id()  通过id我们可以查看到一个变量表示的值在内存中的地址. s = 'alex' print(id(s ...

  6. 基于python的openCV自学笔记(四)——遗漏知识点补充

    本篇补充暑假学opencv遗漏的相关知识点 参考链接:https://www.bilibili.com/video/BV1Fo4y1d7JL?from=search&seid=17628666 ...

  7. 英语——平时遇到重要知识点补充【不断更新中】

    全部知识点请进入:专升本英语--学习笔记[知识点全轻松学习]!!! https://blog.csdn.net/liu17234050/article/details/104576823 目录: 一: ...

  8. cms是什么意思啊_GC 知识点补充——CMS

    之前已经讲过了不少有关 GC 的内容,今天准备将之前没有细讲的部分进行补充,首先要提到的就是垃圾收集器. 基础的回收方式有三种:清除.压缩.复制,衍生出来的垃圾收集器有: Serial 收集器 新生代 ...

  9. c++——抽象类以及string知识点补充

    抽象类与纯虚函数 在这个类当中,我们定义了一个普通的虚函数,并且也定义了一个纯虚函数. 纯虚函数:从上面的定义可以看到,纯虚函数就是没有函数体,同时在定义的时候,其函数名后面要加上"= 0& ...

最新文章

  1. 企业级微服务架构统一安全认证设计与实践!
  2. 降低网站跳出率的技巧分享!
  3. yolo 识别 狗狗自行车
  4. HIVE元数据表/数据字典
  5. android shell用户界面,shell界面下安装和卸载Android应用程序(apk包)
  6. 论文阅读:Spatial Transformer Networks
  7. TCP协议的部分解析(3)
  8. Bartender 让Mac选项列不再拥挤
  9. 【一】如果让我学习TensorFlow,我该怎么学?
  10. 机器学习(二)——xgboost(实战篇)Pima印第安人数据集上的机器学习-分类算法(根据诊断措施预测糖尿病的发病)
  11. 基于C++、MySQL的图书销售管理系统
  12. 如何解决 CAD 启动非常慢的问题
  13. android wear刷机,1分钟搞定刷机 百度DuWear手表系统公测版上线
  14. 玩转字符串篇--代码自动生成,解放双手,android音视频开发
  15. 吴恩达课程作业中的lr_utils下载
  16. JVM面试题(含答案和图和解释)
  17. 一家有两个孩子,已知至少有一个孩子是在星期二出生的男孩。问:两个孩子都是男孩的概率是多大?
  18. 在一个字符串中搜索某个特定的字符值
  19. java学习之屌丝逆袭
  20. 蓝桥杯真题:分巧克力

热门文章

  1. 小米路由器mini刷lede_小米路由器刷Xiaomi Mi WiFi Mini openwrt
  2. PCIe数据传输卡在显卡插槽找不到卡的解决方案
  3. hdparm常用指令
  4. Azure云服务反向DNS
  5. 伪逆学习自动编码器射频干扰去除 Radio frequency interference mitigation using pseudo-inverse learning auto-encoders
  6. 相对全面的四足机器人驱动规划MATLAB和Simulink实现方式(足端摆线规划,Hopf-CPG,Kimura-CPG)
  7. oracle02518,ORA-00600: internal error code [kkmupsViewDestFro_4] in oracle 11.2.0.1
  8. Android 获取ROM信息
  9. L2-L4自动驾驶视觉方案推荐(一)
  10. 计算机绘图实训日志通用篇,cad制图实习日记范例精选