当前位置:网站首页>SQL学习笔记2
SQL学习笔记2
2022-07-01 06:21:00 【小美元】
是对一组数据进行汇总的函数,输入的是一组数据的集合,输出的是单个值,自变量有多个,应变量有多个
常见的聚合函数
sum()avg()类型的字段对函数不适用
max()min()类型对字符串和时间都适用
count统计的个数中不包含空值
avg = sum / count
聚合函数过滤null,如果查询公司的平均奖金率,用avg函数是错误的,因为把空值没有算。正确应该SELECT SUM(commission_pct) / COUNT(IFNULL(commission_pct,0)) FROM employees;
如果需要获得表中准确的数据,那些count更准确呢?
count(“*”),counut(1),count(具体字段)
不同引擎效率不同,但是count(" *" )=count(1)这个效率是最高的
group by的使用
orderby排序,group by 分组操作
一个列进行分组,多个列进行分组
group by声明在from后边
oderby前面,limit前面
group by with rollup 如果是计算各个部门的饿平均公司的话加着它最后汇算一下全部数据的平均工资
withrollup 不适合和orderby进行排序,结果不显示,因为对分组后的部门工资进行排序的时候,rollup不在一个层次上不适合进行排序
having的使用
如果过滤条件中使用了聚合函数,则条件必须使用having来替换where,否则报错,如果出现了having的话,having要求放在group by的后边
开发中使用having的前提是使用了group by
当过滤条件中有聚合函数时则过滤条件必须声明在having中
如果过滤条件中没有聚合函数时则过滤条件必须声明在where
where与having的对比
多表查询的时候where的效率更高
SQL底层的查询原理:
sql语句的执行过程:
首先是from 》 where》group by》having》select》distinct》orderby》limit
SQL的底层执行原理
子查询:
称谓:外查询,内查询,主查询(外查询),子查询(内查询)
分类方式:单行子查询,多行子查询
内查询是否被执行多次:相关子查询,不相关子查询
多行查询的符号:
in 等于列表中的任意一个值
any:需要和单行一起使用,和任意一个值
all:和所有的值
some:any的别名
边栏推荐
- libpng12.so. 0: cannot open shared object file: no such file or directory
- [ManageEngine Zhuohao] what is network operation and maintenance management and what is the use of network operation and maintenance platform
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
- 启牛学堂合作的证券公司是哪家?开户安全吗?
- Minio error correction code, construction and startup of distributed Minio cluster
- DML statement in SQL (data operation language)
- Restframework-simplejwt rewrite authentication mechanism
- Pychart configuring jupyter
- [unity shader custom material panel part II]
- [ManageEngine Zhuohao] helps Julia college, the world's top Conservatory of music, improve terminal security
猜你喜欢

How does the port scanning tool help enterprises?

【#Unity Shader#自定义材质面板_第二篇】

Teach you how to implement a deep learning framework
![[ITSM] what is ITSM and why does it department need ITSM](/img/e1/85b5f00f124829b6a6b40c5cf621bd.png)
[ITSM] what is ITSM and why does it department need ITSM

高阶-二叉搜索树详解

C语言课设学生选修课程系统(大作业)

自开发软件NoiseCreater1.1版本免费试用

Redis安装到Windows系统上的详细步骤

【Unity Shader 描边效果_案例分享第一篇】

Discrimination between left and right limits of derivatives and left and right derivatives
随机推荐
HCM Beginner (II) - information type
Uniapp tree level selector
浅谈SIEM
Pol8901 LVDS to Mipi DSI supports rotating image processing chip
make: g++:命令未找到
Promise
SystemVerilog learning-06-class encapsulation
JDBC connection pool
C#如何打印输出原版数组
地宫取宝(记忆化深搜)
[unity shader custom material panel part II]
SQL中DML语句(数据操作语言)
[leetcode] day91- duplicate elements exist
Pychart configuring jupyter
High order binary search tree
idea 好用插件汇总!!!
ForkJoin和Stream流测试
C语言课设物业费管理系统(大作业)
证券类开户有什么影响 开户安全吗
[summary of problem thinking] Why is the register reset performed in user mode?