当前位置:网站首页>mysql表的增删改查(进阶)
mysql表的增删改查(进阶)
2022-07-02 09:42:00 【菜菜不恰菜】

目录
🥬新增
insert into B select * from A;//将A表的信息通过查询新增到B表中去![]()
🥬聚合查询
count;//返回到查询的数据总和

sum;//返回到查询的数据总和(只对数字有意义)
只对数字有意义

avg/max/min;//返回查询数据的平均值/最大值/最小值(只对数字有意义)
🥬分组查询
select * from 表名 group by 分组条件;
这里是先执行分组,再根据分组执行每个组的聚合函数。
🥬条件查询
having;group by 子句进行分组以后,需要对分组结果再进行条件过滤时,就可以使用having。where是在分组之前执行,如果要对分组之后的结果进行条件筛选,就需要使用having(having搭配group by使用)。
例如:求每种角色的平均薪资,除了吴九。(这里就是用where,分组之前指定条件,先去除吴九,在分组求平均薪资。

求每种角色平均薪资,只保留平均薪资10000以下的,这里就用having。要先求出平均薪资才能进行筛选。

🥬联合查询
第一种写法:select * from 表名1,表名2;
第二种写法:select * from 表名1 join 表名2 on 条件;联合查询(较重要)是多表查询,前面的查询都是单表查询。多表查询中的核心操作---笛卡尔积。
笛卡尔积的运算就是将两个表的每条记录分别进行组合,得到一组新的记录。

以上记录并不都是我们想要的结果,我们可以通过筛选得到我们想要的结果。


那么join on后面跟条件和 用where 跟条件有什么区别呢?
from多个表where写法叫做“内连接"。
使用 join on的写法,既可以表示内连接,还可以表示外连接。
select 列名 from 表1 inner join 表2 on条件;inner join表示是"内连接"其中inner可以省略。
select 列名 from 表1 left join 表2 on条件;左外连接。
select列from表1 right join表2 on条件;右外连接。

🥬自连接
select s1.student_id,s1.score,s2.score from score as s1,score as s2 where s1.student_id=s2.student_id and s1.course_id=3 and s2.course_id=1 and s1.score>s2.score;🥬合并查询
union;//这个可自动去重
union all;//这个不可自动去重该操作符用于取得两个结果集的并集。
例如:查询id小于3,或者名字为“英文”的课程。
select * from course where id<3 union select * from course where name='英文'; 或者使用or来实现
select * from course where id<3 or name='英文';🥬小结
以上就是今天的内容了,有什么问题大家都可以在评论区留言

边栏推荐
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- Easyexcel and Lombok annotations and commonly used swagger annotations
- Three transparent LED displays that were "crowded" in 2022
- CONDA common command summary
- 【2022 ACTF-wp】
- On data preprocessing in sklearn
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- Mish shake the new successor of the deep learning relu activation function
- Implementation of address book (file version)
- The position of the first underline selected by the vant tabs component is abnormal
猜你喜欢

自然语言处理系列(二)——使用RNN搭建字符级语言模型

From scratch, develop a web office suite (3): mouse events

动态内存(进阶四)

CONDA common command summary

Natural language processing series (II) -- building character level language model using RNN

YYGH-9-预约下单

机械臂速成小指南(七):机械臂位姿的描述方法

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

Seriation in R: How to Optimally Order Objects in a Data Matrice

Flesh-dect (media 2021) -- a viewpoint of material decomposition
随机推荐
Log4j2
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
Flesh-dect (media 2021) -- a viewpoint of material decomposition
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
R HISTOGRAM EXAMPLE QUICK REFERENCE
easyExcel和lombok注解以及swagger常用注解
(C语言)八进制转换十进制
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
YYGH-BUG-04
GGPlot Examples Best Reference
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
Leetcode14 最长公共前缀
The computer screen is black for no reason, and the brightness cannot be adjusted.
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)
xss-labs-master靶场环境搭建与1-6关解题思路
PyTorch搭建LSTM实现服装分类(FashionMNIST)
conda常用命令汇总
B high and beautiful code snippet sharing image generation
Orb-slam2 data sharing and transmission between different threads
