当前位置:网站首页>MySQL common statements
MySQL common statements
2022-07-31 16:39:00 【xbhog】
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第1天,点击查看活动详情
SQL
执行语句顺序:select –>where –> group by–> having–>order by
UNION【ALL】
:
- 使用
UNION
可以实现将多个查询结果集合并为一个结果集. ALL
:Said in the results of several don't remove duplicate records- 没有指定
ALL
:Remove the merged result set duplicate records - 所有查询语句中列的个数和列的顺序必须相同.
- 所有查询语句中对应列的数据类型必须兼容.
- ORDER BY语句要放在最后一个查询语句的后边.
- 使用
- 时间处理:
YEAR/MONTH
函数等date_format()
函数:date_format(date, "%Y-%m")="202108"day(last_day(xxxx))
How many days in the current month:last_day()
函数:返回月份中的最后一天.
- 模糊匹配:
like _
单个like %
多个- 正则:
regexp
instr(M,S)
M 模板串,S子串; 返回结果:<= 0不存在,> 0存在- likeFuzzy matching multiple
like %xxx%xxx%
like %xxx% and like %yyy%
- 联结:
- 内联结:
inner join
.取两列的交集. - 外联结:
- 内联结:
left join
:左连接,以左边表的列为主,取两列的交集,对于不在右边列存在的名称取null.right join
:右连接,以右边表的列为主,取两列的交集,对于不在左边列存在的名称取null.
- 统计(
count
函数)
count(*)
包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULLcount(1)
包括了忽略所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULLcount(列名)
只包括列名那一列,在统计结果的时候,会忽略列值为空(这里的空不是只空字符串或者0,而是表示null)的计数,即某个字段值为NULL时,不统计.
- Data update or insert(
replace
)
- 通过主键 / 唯一索引,First remove the source data(若存在),再插入数据(格式与
insert into
相似) replace into
:有就覆盖,没有就插入
- 通过主键 / 唯一索引,First remove the source data(若存在),再插入数据(格式与
timestampdiff
函数
TIMESTAMPDIFF(interval, time_start, time_end)
可计算time_start-time_end
的时间差,单位以指定的interval为准,常用可选:
- SECOND 秒
- MINUTE 分钟(返回秒数差除以60的整数部分)
- HOUR 小时(返回秒数差除以3600的整数部分)
- DAY 天数(返回秒数差除以3600*24的整数部分)
- MONTH 月数
- YEAR 年数
- 删除数据:
- 根据条件删除:
DELETE FROM tb_name [WHERE options] [ [ ORDER BY fields ] LIMIT n ]
TRUNCATE tb_name
:全部删除(表清空,包含自增计数器重置)
- 根据条件删除:
- DDL(数据定义语言)
- 添加列
alter table 表名 add column 列名 类型 【first|after 字段名】
;
- 修改列的类型或约束
alter table 表名 modify column 列名 新类型 【新约束】
;
- 修改列名
alter table 表名 change column 旧列名 新列名 类型
;
- 删除列
alter table 表名 drop column 列名
;
- 修改表名
alter table 表名 rename 【to】 新表名
;
- 将某一列放到第一列
alter table 表名 modify column 列名 类型 first
;
- 删除表
drop table【if exists】 表名
;
- 复制表结构
create table 表名 like 旧表
;
- 复制表的结构+数据
create table 表名
select 查询列表 from 旧表【where 筛选】
;
- 四舍五入(
ROUND
) ROUND(X)
,返回参数 X With the integerROUND(X,D)
,返回 X ,其值保留到小数点后 D 位,第 D 位的保留方式为四舍五入
边栏推荐
- 基于C语言的编译器设计与实现
- Mariabackup implements incremental data backup for Mariadb 10.3
- 对话庄表伟:开源第一课
- 多主复制下处理写冲突(3)-收敛至一致的状态及自定义冲突解决逻辑
- Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
- Website vulnerability repair service provider's analysis of unauthorized vulnerability
- Qt实战案例(54)——利用QPixmap设计图片透明度
- jeecg master-slave database read-write separation configuration "recommended collection"
- 二分查找的细节坑
- 牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
猜你喜欢
The 2nd China PWA Developer Day
研发过程中的文档管理与工具
上传图片-微信小程序(那些年的坑记录2022.4)
GP 6 overall architecture study notes
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
Graham‘s Scan法求解凸包问题
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
6-22漏洞利用-postgresql数据库密码破解
随机推荐
MySQL multi-table union query
牛客网刷题(四)
对话庄表伟:开源第一课
深度学习机器学习理论及应用实战-必备知识点整理分享
6. 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务
2020微信小程序反编译教程(小程序反编译源码能用吗)
AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
最后写入胜利(丢弃并发写入)
网站漏洞修复服务商关于越权漏洞分析
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
[TypeScript]OOP
基于C语言的编译器设计与实现
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
多主复制的适用场景(1)-多IDC
ML.NET related resources
Smart Trash Can (8) - Infrared Tube Sensor (Raspberry Pi pico)
jeecg master-slave database read-write separation configuration "recommended collection"
Baidu cloud web speed playback (is there any website available)
The arm button controls the flashing of the led light (embedded button experiment report)
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 失败