当前位置:网站首页>MySQL中的文本處理函數整理,收藏速查
MySQL中的文本處理函數整理,收藏速查
2022-07-04 07:25:00 【Python和數據分析】
大家好,我是翔宇!
前言
今天整理了一下MySQL中中的文本處理函數,當然如果翔宇整理漏掉了麻煩後臺留言。不管是在哪一個編程語言,對文本的處理都是及其重要的,因為大家都是知道,日常遇到的數據中,文本都是會占很大一部門的,因此,大佬們就直接把常用到的處理操作進行封裝,這樣就不需要大家在進行文本處理時還要自己寫函數,而且實際上這樣還為很多剛入門的小夥伴給予了友好的感覺,比如很多數據分析師
學習SQL只是想要取一下數,壓根就不算學習啥視圖、存儲過程、函數,所以這個時候,如果能够直接進行使用封裝好的內置函數,那簡直是小夥伴們的福音啊,說了這麼多,就是想要說文本處理函數是很重要的,當然,翔宇這裏已經整理了一下,你可以收藏一下,用到時候進行速查就行,不嫌麻煩你也可以像翔宇一樣進行嘗試整理一下。
1.思維導圖

2.使用實例
下面的例子不具體查錶,直接用select進行測試。
2.1 截取與拼接
left: 左截取,從前面(左邊)向後對字符串截取一定長度的子串,語法:left(字符串,截取長度)
select left('滾滾長江東逝水',4); -- 返回“滾滾長江”
right: 右邊截取,從後面(右邊)向前對字符串截取一定長度的子串,語法:right(字符串,截取長度)
select right('滾滾長江東逝水',4); -- 返回“江東逝水”
substring : 對字符串進行制定比特置和長度的截取子串,語法:substring(字符串,從第幾個字符開始[,截取長度])
注意:語法中的[]錶示此參數可缺省
此函數第三個參數可缺省(不填),缺省時錶示截取到最後一個字符
select substring('滾滾長江董事會',3); -- 返回“長江董事會”
select substring('滾滾長江都是水',3,2); -- 返回“長江”
concat:字符串拼接,語法:concat(字符串1[,字符串2,字符串3,…])
此函數的參數可以給一個或者多個,當參數有null時,結果也返回null
select concat('滾滾') -- 返回 “滾滾”
select concat('滾滾',null) --返回 null
select concat('滾滾','長江','東逝','水') -- 返回 “滾滾長江東逝水”
2.2轉換
lower:轉換為小寫,語法:lower(待轉換字符串)
select lower('HELLO,WORLD'); -- 返回 “hello,world”
upper :轉換為大寫,語法:upper(待轉換字符串)
select upper('hello,world'); -- 返回 "HELLO,WORLD"
2.3長度
length :返回字符串的長度 ,語法:length(字符串)
select length('滾滾長江東逝水'); -- 返回21(一個漢字占三個英文字符的長度)
select length('hello'); -- 返回5
select length(null) -- 返回null
2.4查找子串比特置
locate:子串在原字符串中能找到時返回其在原字符串中的第一個比特置 ,語法 Locate(子串,父串)
說明:子串不存在返回0,子串為null返回null
select locate('sql','翔宇在學習mysql還是哪種sql'); -- 返回8,第一個s是第八個字符
select locate('pgsql','翔宇在學習mysql還是哪種sql'); -- 返回0
select locate(null,'翔宇在學習mysql還是哪種sql'); -- 返回null
position :和locate功能一樣,寫法不同,語法position(substr in str)
select position('sql' in '翔宇在學習mysql還是哪種sql'); -- 返回8
instr:功能和前面兩個一樣,寫法不同,語法:instr(str,substr)
select instr('翔宇在學習mysql還是哪種sql','sql'); -- 返回8
2.5去空格
ltrim:去掉字符串中的前導空格,就是開頭有空格就去掉,語法:ltrim(字符串)
select ltrim(' 現在是淩晨2點 '); -- 返回“現在是淩晨2點 ”
rtrim:去掉字符串的尾部空格,語法:rtrim(字符串)
select rtrim(' 現在是淩晨兩點 '); -- 返回“ 現在是淩晨兩點”
trim:去除字符串中前導和結尾空格,但並不能去除中間的空格 ,語法:trim(字符串)
select trim(' 現在是 淩晨 兩點 '); -- 返回 “現在是 淩晨 兩點”
2.6填充
lpad:對字符串進行固定長度返回,超過固定長度的截取,低於固定長度的用指定的字符左填充,語法:lpad(字符串,固定長度,需要填充的字符)
低於固定長度時
select lpad('一gao窩裏gaogao',20,'*'); -- 返回 "********一gao窩裏gaogao"
超過固定長度時
select lpad('法外狂徒張三',4,'^^'); -- 返回“法外狂徒”
rpad:對字符串進行固定長度返回,超過固定長度的截取,低於固定長度的用指定的字符右填充,語法:rpad(字符串,固定長度,需要填充的字符)
低於固定長度時
select rpad('一gao窩裏gaogao',20,'*'); -- 返回 "一gao窩裏gaogao********"
超過固定長度時
select rpad('法外狂徒張三',4,'^^'); -- 返回“法外狂徒”
2.7替換
replace:將字符串中的子串進行替換成新子串後進行返回,語法:replace(原字符串,待替換的子串,用於替換的子串)
select replace('滾滾長江東逝水','滾','不滾'); -- 返回“不滾不滾長江東逝水”
注意:進行替換時會全部替換,不可設定替換第幾個滿足條件的子串
2.8 返回發音
soundex:返回字符串的語音錶示形式soundex,有助於比較拼寫不同但英語發音相似的單詞,語法:soundex(字符串)
例如:
如果庫中存在一名名為Y.LEE的客戶而搜索的時候的輸入錯誤,下面的sql是不會有任何返回結果的。
SELECT CUSTOMER_NAME FROM CUSTOMER WHERE CUSTOMER_NAME = 'Y LEE'
而如果這樣寫:
SELECT CUSTOMER_NAME FROM CUSTOMER WHERE SOUNDEX(CUSTOMER_NAME) =SOUNDEX('Y LEE')
因為兩者發音相似,所以他們的SOUNDEX值匹配,這樣就會返回一條數據。
soundex參考於:https://www.cnblogs.com/shuoli/p/8099212.html
好了,sql的文本函數就總結到這裏,歡迎大家關注加收藏,同時也歡迎大家光臨翔宇的同名公眾號,翔宇持續更新中!
边栏推荐
- Flink memory model, network buffer, memory tuning, troubleshooting
- Adaptive spatiotemporal fusion of multi-target networks for compressed video perception enhancement
- Zephyr 学习笔记1,threads
- 云Redis 有什么用? 云redis怎么用?
- NLP literature reading summary
- What is the use of cloud redis? How to use cloud redis?
- 【FreeRTOS】FreeRTOS学习笔记(7)— 手写FreeRTOS双向链表/源码分析
- jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
- Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx
- Types of references in BibTex
猜你喜欢

com. alibaba. nacos. api. exception. NacosException

flask-sqlalchemy 循环引用

Adaptive spatiotemporal fusion of multi-target networks for compressed video perception enhancement

What is industrial computer encryption and how to do it

What is the use of cloud redis? How to use cloud redis?

果果带你写链表,小学生看了都说好

Vulhub vulnerability recurrence 77_ zabbix
![[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph](/img/5e/7ce21dd544aacf23b4ceef1ec547fd.png)
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph

The IP bound to the socket is inaddr_ The meaning of any htonl (inaddr_any) (0.0.0.0 all addresses, uncertain addresses, arbitrary addresses)

Routing decorator of tornado project
随机推荐
Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx
[network data transmission] FPGA based development of 100M / Gigabit UDP packet sending and receiving system, PC to FPGA
Lottery system test report
Cell reports: Wei Fuwen group of the Institute of zoology, Chinese Academy of Sciences analyzes the function of seasonal changes in the intestinal flora of giant pandas
Splicing plain text into JSON strings - easy language method
Summary of MySQL common judgment functions!! Have you used it
Knowledge payment applet dream vending machine V2
2022-021ARTS:下半年開始
Used on windows Bat file startup project
Zephyr learning notes 1, threads
Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
Unity opens the explorer from the inspector interface, selects and records the file path
Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
"Sword finger offer" 2nd Edition - force button brush question
Blog stop statement
Label management of kubernetes cluster
Routing decorator of tornado project
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Electronic Association C language level 1 34, piecewise function
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘