网站首页 > 教程文章 正文
前言:
在MYSQL数据库的使用中,业务数据的增删改查离不开数据治理,当然,这就会涉及到数据库系统内置的一些文本处理函数的使用,来实现数据的规范处理,达到业务应用的目的,涉及到的方法可能包含以下内容:
1、替换:replace(需替换的字段,被替换的字符,替换的内容)
2、截取:substr(需处理的数字和字符串,从1开始的下标序号,截取字符的长度)
3、分割:substring_index(字段,分割符号,M) M指第几个分割符,有正负之分
4、拼接:concat(字段1,字段2,字段3,......)
5、字串检索:locate(待检索的字串,字段,检索起始位置[数字,默认1])
6、小写转换:lower(待转换的字串)
7、大写转换:upper(待转换的字串)
8、长度返回:length(待计算长度的字串)
9、左截取:left(待提取的字符串,指定将从左边返回的字符数)
10、右截取:right(待提取的字符串,指定将从右边返回的字符数)
11、删除指定字串:trim(both| leading | trailing 子串 from 原始字段)
12、清除左空格:ltrim(需要删除左侧空格的字符串)
13、清除右空格:rtrim(需要删除右侧空格的字符串)
14、返回字符串的位置:position(在字符串中搜索的子串 in 要搜索的原始字符串)
模拟数据:
应用1、替换:replace(需替换的字段,被替换的字符,替换的内容)
select id,other_name,replace(other_name,'_','替换内容') from class_score where id = 2
查询结果:
应用2、截取:substr(需处理的数字和字符串,从1开始的下标序号,截取字符的长度)
select id,bobby,substr(bobby,1,4) from class_score where id = 2
查询结果:
应用3、分割:substring_index(字段,分割符号,M) M指第几个分割符,有正负之分
select id,bobby,substring_index(bobby,',',1) from class_score where id = 2 //若M数字为正,从左往右,查询的是第M分隔符左边所有的内容
查询结果:
select id,bobby,substring_index(bobby,',',-1) from class_score where id = 2 ////若M数字为负,从右往左,查询的是第|M|分隔符右边所有的内容
查询结果:
应用4、拼接:concat(字段1,字段2,字段3,......)
select id,name,six,subject,concat(name,'-',six,'-',subject) as desc_ from class_score
查询结果:
应用5、字串检索:locate(待检索的字串,字段,检索起始位置[数字,默认1])
select id,name,other_name,locate('a',other_name,2) as desction from class_score where id = 12
从other_name查找a第一次出现的位置。如果a不在other_name中,则返回值为0。
查询结果:
应用6、小写转换:lower(待转换的字串)
select id,name,other_name,lower(other_name) from class_score where id = 2
查询结果:
应用7、大写转换:upper(待转换的字串)
select id,name,other_name,upper(other_name) from class_score where id = 1
查询结果:
应用8、长度返回:length(待计算长度的字串)
select id,name,other_name,length(other_name) from class_score where id = 2
查询结果:
应用9、左截取:left(待提取的字符串,指定将从左边返回的字符数)
select id,name,other_name,left(other_name,2) from class_score where id = 12
查询结果:
应用10、右截取:right(待提取的字符串,指定将从右边返回的字符数)
select id,name,other_name,right(other_name,3) from class_score where id = 12
查询结果:
应用11、删除指定字串:trim(both| leading | trailing 子串 from 原始字段)
SELECT
trim( ' xhx ' ) AS '删除指定字符串中的空格',
trim( LEADING 'x' FROM 'xxhhxx' ) AS '删除指定的首字符',
trim( BOTH 'x' FROM 'xxxhhhxxxhhhxxx' ) AS '删除指定的首尾字符',
trim( TRAILING 'x' FROM 'xxxhhhxxxhhhxxx' ) AS '删除指定的尾字符'
查询结果:
应用12、清除右空格:rtrim(需要删除右侧空格的字符串)
SELECT rtrim( ' xhx ' ) AS '清除右边空格',REPLACE ( rtrim( ' xhx ' ), ' ', '-' ) AS '验证结果'
查询结果:
应用13、清除左侧空格:ltrim(需要删除左侧空格的字符串)
SELECT ltrim( ' xhx ' ) AS '清除左侧空格',REPLACE ( ltrim( ' xhx ' ), ' ', '-' ) AS '验证结果'
查询结果:
应用14、返回字符串的位置:position(在字符串中搜索的子串 in 要搜索的原始字符串)
SELECT POSITION('a' in 'saggxx') as '字符a出现的位置' //若字符串没有所查询的字符则返回0
查询结果:
猜你喜欢
- 2025-03-14 CAS单点登录(第7版)6.认证(cas单点退出)
- 2025-03-14 Dockerfile构建mysql8.0.27数据库
- 2025-03-14 Docker篇(五):容器之间该如何通讯?
- 2025-03-14 MySql高可用集群MySQL Router负载均衡读写分离
- 2025-03-14 MySQL MGR集群原理及实践(mysql集群架构)
- 2025-03-14 「原创」基于CentOS环境下,LNMP+REDIS+YAF+Java环境搭建
- 2025-03-14 MySQL系列-源码编译安装(v8.0.25)
- 2025-03-14 MySQL海量数据优化(理论+实战)(mysql数据库优化方案)
- 2025-03-14 RxSqlUtils(base R2dbc)(r2dbc-mysql)
- 2025-03-14 《基于Docker的MySQL主从复制:快速搭建高可用从库》
- 最近发表
-
- 网络安全干货知识 | 手把手搭建 k8s docker 漏洞环境
- docker+k8s 报错(k8s docker login)
- K8s 集群运行时:从 Docker 升级到 Containerd
- 轻松掌握k8s安装(使用docker)知识点
- 什么是 k8s(Kubernetes)?Docker 与 Kubernetes选择哪一个?
- 从 Docker 到 K8s:初学者常见的误区盘点
- Docker容器是什么?K8s和它有什么关系呢?
- Docker 是什么? 它与K8S之间是什么关系?
- Docker是什么?K8s是什么?如何从0到1实现Docker与K8s全流程部署
- K8S与Docker的区别(k8s与docker的区别是啥)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)