当前位置:网站首页>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='英文';
🥬小结
以上就是今天的内容了,有什么问题大家都可以在评论区留言
边栏推荐
- Leetcode922 按奇偶排序数组 II
- 史上最易懂的f-string教程,收藏這一篇就够了
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- Natural language processing series (III) -- LSTM
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- (C语言)八进制转换十进制
- SSH automatically disconnects (pretends to be dead) after a period of no operation
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- Log4j2
- How to Easily Create Barplots with Error Bars in R
猜你喜欢
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
PyTorch nn.RNN 参数全解析
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Take you ten days to easily finish the finale of go micro services (distributed transactions)
How to Create a Beautiful Plots in R with Summary Statistics Labels
Natural language processing series (II) -- building character level language model using RNN
自然语言处理系列(三)——LSTM
YYGH-BUG-05
The selected cells in Excel form have the selection effect of cross shading
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
随机推荐
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
小程序链接生成
How does Premiere (PR) import the preset mogrt template?
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Yygh-9-make an appointment to place an order
Larvel modify table fields
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
How to Add P-Values onto Horizontal GGPLOTS
How to Easily Create Barplots with Error Bars in R
机械臂速成小指南(七):机械臂位姿的描述方法
K-Means Clustering Visualization in R: Step By Step Guide
ES集群中节点与分片的区别
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
YYGH-BUG-04
Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
SSH automatically disconnects (pretends to be dead) after a period of no operation
自然语言处理系列(三)——LSTM
Le tutoriel F - String le plus facile à comprendre de l'histoire.
to_bytes与from_bytes简单示例