👉 這是一個或許對你有用的社群
《專案實戰(影片)》:從書中學,往事上“練” 《網際網路高頻面試題》:面朝簡歷學習,春暖花開 《架構 x 系統設計》:摧枯拉朽,掌控面試高頻場景題 《精進 Java 學習指南》:系統學習,網際網路主流技術棧 《必讀 Java 原始碼專欄》:知其然,知其所以然

👉這是一個或許對你有用的開源專案國產 Star 破 10w+ 的開源專案,前端包括管理後臺 + 微信小程式,後端支援單體和微服務架構。功能涵蓋 RBAC 許可權、SaaS 多租戶、資料許可權、商城、支付、工作流、大屏報表、微信公眾號、ERP、CRM、AI 大模型等等功能:
Boot 多模組架構:https://gitee.com/zhijiantianya/ruoyi-vue-pro Cloud 微服務架構:https://gitee.com/zhijiantianya/yudao-cloud 影片教程:https://doc.iocoder.cn 【國內首批】支援 JDK 17/21 + SpringBoot 3.3、JDK 8/11 + Spring Boot 2.7 雙版本
-
前言 -
1.group_concat -
2.char_length -
3.locate -
4.replace -
5.now -
6.insert into … select -
7.insert into … ignore -
8.select … for update -
9.on duplicate key update -
10.show create table -
11.create table … select -
12.explain -
13.show processlist -
14.mysqldump

前言
MYSQL
資料庫挺多的,發現了一些非常有用的小玩意,今天拿出來分享到大家,希望對你會有所幫助。基於 Spring Boot + MyBatis Plus + Vue & Element 實現的後臺管理系統 + 使用者小程式,支援 RBAC 動態許可權、多租戶、資料許可權、工作流、三方登入、支付、簡訊、商城等功能
專案地址:https://github.com/YunaiV/ruoyi-vue-pro 影片教程:https://doc.iocoder.cn/video/
1.group_concat
group by
進行分組的場景,是非常多的。selectnamefrom`user`
groupbyname
;
group_concat
函式。selectname
,
group_concat
(code)
from`user`
groupbyname
;

group_concat
函式,可以輕鬆的把分組後,name相同的資料拼接到一起,組成一個字串,用逗號
分隔。基於 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現的後臺管理系統 + 使用者小程式,支援 RBAC 動態許可權、多租戶、資料許可權、工作流、三方登入、支付、簡訊、商城等功能
專案地址:https://github.com/YunaiV/yudao-cloud 影片教程:https://doc.iocoder.cn/video/
2.char_length
長度
,然後根據字元的長度進行排序
。char_length
。select
*
from
brand
wherenamelike'%蘇三%'
orderbychar_length
(
name
)
asclimit5
;

模糊查詢
之後,再使用char_length
函式獲取name欄位的字元長度,然後按長度升序
。3.locate
蘇三
,需要明確知道它在某個字串中的位置時,該怎麼辦呢?locate
函式。select
*
from
brand
wherenamelike'%蘇三%'
orderbychar_length
(
name
)
asc
,
locate
(
'蘇三'
,
name
)
asclimit5
,
5
;

instr
和position
函式,它們的功能跟locate
函式類似,在這裡我就不一一介紹了,感興趣的小夥伴可以找我私聊。4.replace
replace
函式。update
brand
setname
=
REPLACE
(
name
,
'A'
,
'B'
)
whereid
=
1
;
前後空格
:update
brand
setname
=
REPLACE
(
name
,
' '
,
''
)
wherenamelike' %'
;
update
brand
setname
=
REPLACE
(
name
,
' '
,
''
)
wherenamelike'% '
;
json格式
的資料內容,真的非常有用。5.now
當前時間
,可以使用now()
函式,例如:selectnow
()
from
brand
limit1
;

年月日時分秒
。毫秒
,可以使用now(3)
,例如:selectnow
(
3
)
from
brand
limit1
;

6.insert into … select
插入資料
。INSERTINTO`brand`
(
`id`
,
`code`
,
`name`
,
`edit_date`
)
VALUES
(
5
,
'108'
,
'蘇三'
,
'2022-09-02 19:42:21'
);
insert into ... select
語法。INSERTINTO`brand`
(
`id`
,
`code`
,
`name`
,
`edit_date`
)
selectnull
,code,
name
,
now
(
3
)
from`order`where
code
in
(
'004'
,
'005'
);
7.insert into … ignore
INSERTINTO`brand`
(
`id`
,
`code`
,
`name`
,
`edit_date`
)
VALUES
(
123
,
'108'
,
'蘇三'
,
now
(
3
));

not exists
語句,也能達到防止出現重複資料的目的,比如:INSERTINTO`brand`
(
`id`
,
`code`
,
`name`
,
`edit_date`
)
selectnull
,
'108'
,
'蘇三'
,
now
(
3
)
from
dual
wherenotexists
(
select
*
from`brand`wherename
=
'蘇三'
);
insert into ... ignore
語法。INSERTignoreINTO`brand`
(
`id`
,
`code`
,
`name`
,
`edit_date`
)
VALUES
(
123
,
'108'
,
'蘇三'
,
now
(
3
));
8.select … for update
悲觀鎖
,它是一種排它鎖,根據鎖的粒度從大到小分為:表鎖
、間隙鎖
和行鎖
。select ... for update
語法了。begin
;
select
*
from`user`whereid
=
1
forupdate
;
//業務邏輯處理
update`user`set
score=score
-1whereid
=
1
;
commit
;
for update
鎖住一行記錄,其他事務就不能在該事務提交之前,去更新那一行的資料。主鍵
或者唯一索引
,不然行鎖可能會失效,有可能變成表鎖
。9.on duplicate key update
加唯一索引
、加分散式鎖
等。on duplicate key update
語法。INSERTINTO`brand`
(
`id`
,
`code`
,
`name`
,
`edit_date`
)
VALUES
(
123
,
'108'
,
'蘇三'
,
now
(
3
))
onduplicatekeyupdatename
=
'蘇三'
,edit_date=
now
(
3
);
on duplicate key update
語法,可能會存在死鎖
的問題,所以要根據實際情況酌情使用。10.show create table
desc
命令,比如:
desc `order`;

show index
命令。showindexfrom`order`
;

show create table
命令了。showcreatetable`order`
;

Table
表示表名
,Create Table
就是我們需要看的建表資訊
,將資料展開:

我們能夠看到非常完整的建表語句,表名、欄位名、欄位型別、欄位長度、字元集、主鍵、索引、執行引擎等都能看到。
11.create table … select
-
建立一張臨時表 -
將資料插入臨時表
createtable
order_2022121819
like`order`
;
新表
,只是該表的資料為空
而已。insertinto
order_2022121819
select
*
from`order`
;
create table ... select
命令。createtable
order_2022121820
select
*
from`order`
;
表備份
。12.explain
索引
執行情況。explain
命令,檢視mysql的執行計劃
,它會顯示索引的使用情況
。explainselect
*
from`order`where
code=
'002'
;



13.show processlist
show processlist
命令檢視當前執行緒執行情況
。
-
id 執行緒id -
User 執行sql的賬號 -
Host 執行sql的資料庫的ip和端號 -
db 資料庫名稱 -
Command 執行命令,包括:Daemon、Query、Sleep等。 -
Time 執行sql所消耗的時間 -
State 執行狀態 -
info 執行資訊,裡面可能包含sql資訊。
14.mysqldump
mysqldump
工具,該工具會將資料查出來,轉換成insert語句,寫入到某個檔案中,相當於資料備份
。資料還原
。mysqldump -h主機名 -P埠 -u使用者名稱 -p密碼 引數1,引數2.... > 檔名稱.sql
mysqldump -h 192.22.25.226 -u root -p123456 dbname > backup.sql





