当前位置:网站首页>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='英文';
🥬小结
以上就是今天的内容了,有什么问题大家都可以在评论区留言
边栏推荐
- 二分刷题记录(洛谷题单)区间的甄别
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- xss-labs-master靶场环境搭建与1-6关解题思路
- Natural language processing series (I) -- RNN Foundation
- Power Spectral Density Estimates Using FFT---MATLAB
- Cmake cross compilation
- qt 仪表自定义控件
- conda常用命令汇总
- PyTorch nn.RNN 参数全解析
- HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
猜你喜欢
YYGH-BUG-04
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)
[visual studio 2019] create and import cmake project
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
Power Spectral Density Estimates Using FFT---MATLAB
【2022 ACTF-wp】
HOW TO ADD P-VALUES TO GGPLOT FACETS
小程序链接生成
随机推荐
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
MSI announced that its motherboard products will cancel all paper accessories
[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
Natural language processing series (I) -- RNN Foundation
conda常用命令汇总
Leetcode922 按奇偶排序数组 II
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
B high and beautiful code snippet sharing image generation
Leetcode209 长度最小的子数组
YYGH-9-预约下单
YYGH-BUG-05
YYGH-10-微信支付
Power Spectral Density Estimates Using FFT---MATLAB
动态内存(进阶四)
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Depth filter of SvO2 series
时间格式化显示
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)