当前位置:网站首页>MySQL常用语句整理
MySQL常用语句整理
2022-08-02 00:11:00 【林非凡1998】
SQL执行语句:**select –>where –> group by–> having–>order by**UNION【ALL】:- 使用
UNION可以实现将多个查询结果集合并为一个结果集。 ALL:表示在结果几种不去除重复的记录- 没有指定
ALL:去除合并后结果集中重复的记录 - 所有查询语句中列的个数和列的顺序必须相同。
- 所有查询语句中对应列的数据类型必须兼容。
- ORDER BY语句要放在最后一个查询语句的后边。
- 使用
- 时间处理:
YEAR/MONTH函数等date_format()函数:date_format(date, "%Y-%m")="202108"day(last_day(xxxx))当前月份中有多少天:last_day()函数:返回月份中的最后一天。
- 模糊匹配:
like _单个like %多个- 正则:
regexp instr(M,S)M 模板串,S子串; 返回结果:<= 0不存在,> 0存在- like模糊匹配多个
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时,不统计。
- 数据更新或插入(
replace)- 通过主键 / 唯一索引,先删除源数据(若存在),再插入数据(格式与
insert into相似) replace into:有就覆盖,没有就插入
- 通过主键 / 唯一索引,先删除源数据(若存在),再插入数据(格式与
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 最近似的整数ROUND(X,D),返回 X ,其值保留到小数点后 D 位,第 D 位的保留方式为四舍五入
边栏推荐
- PHP to read data from TXT file
- c语言字符和字符串函数总结(二)
- Axure tutorial - the new base (small white strongly recommended!!!)
- An overview of the most useful DeFi tools
- 这 4 款电脑记事本软件,得试试
- PHP从txt文件中读取数据的方法
- 图解LeetCode——1161. 最大层内元素和(难度:中等)
- Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
- 协作乐高 All In One:DAO工具大全
- Active Disturbance Rejection Control of Substation Inspection Robot Based on Data Drive
猜你喜欢

Short video SEO search operation customer acquisition system function introduction

When Netflix's NFTs Forget Web2 Business Security

How to find new potential projects?Tools recommended

【HCIP】BGP小型实验(联邦,优化)

These 4 computer notepad software, you have to try

22. The support vector machine (SVM), gaussian kernel function

poker question
![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)
[21-Day Learning Challenge] A small summary of sequential search and binary search

SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture

Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
随机推荐
【HCIP】BGP小型实验(联邦,优化)
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
CRS management and maintenance
Are test points the same as test cases?
JSP out.write()方法具有什么功能呢?
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
Redis-消息发布订阅
什么是低代码(Low-Code)?低代码适用于哪些场景?
字符串分割函数strtok练习
JSP如何使用page指令让JSP文件支持中文编码呢?
JSP out. The write () method has what function?
Cyber-Physical System State Estimation and Sensor Attack Detection
async/await 原理及执行顺序分析
els block boundary deformation processing
Using the "stack" fast computing -- reverse polish expression
基于数据驱动的变电站巡检机器人自抗扰控制
Play NFT summer: this collection of tools is worth collecting
08-SDRAM:汇总
2022/08/01 学习笔记 (day21) 泛型和枚举
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture