当前位置:网站首页>MySQL parsing serialized fields
MySQL parsing serialized fields
2022-06-13 08:19:00 【Parthenocissus still works】

Sometimes you need to query statistics similar to those in the above table ext Field in the serialized string key The corresponding value is troublesome , Ordinary sql Cannot query directly , You can't add conditions to query ,mysql There is no relevant parsing method , Therefore, it is written as follows mysql Method :
First write a little demo The beta :
create function yangstr(str varchar(255), tar varchar(50)) returns varchar(255) no sql begin declare pos int; declare secstr varchar(255); declare finstr varchar(255); set pos = locate(tar,str); if pos = 0 then return null; else set secstr = substr(str,pos + LENGTH(tar) + 2); set finstr = substr(secstr,locate(':',secstr)+1,locate(';',secstr) - 1 - locate(':',secstr)); set finstr = trim(BOTH '"' FROM finstr); return finstr; end if; end$$
Then I wrote a recursive method to deal with the existence of arrays , however mysql function Recursion is not supported , So it is changed to the following loop processing :
create function yangstr(str varchar(255), tar varchar(50)) returns varchar(255) no sql begin
declare pos int; declare pointpos int; declare secstr varchar(255); declare tartype char(2); declare fircolon int; declare seccolon int; declare finstr varchar(255); declare currtar varchar(50);declare nest boolean default true;
while nest do
set pointpos = locate('.',tar);
if pointpos = 0 then
# Basic types
set pos = locate(tar,str);
if pos = 0 then
return null;
else
set secstr = substr(str,pos + LENGTH(tar) + 2);
set tartype=left(secstr,2);
if tartype = 'i:' then
set finstr = substr(secstr,locate(':',secstr)+1,locate(';',secstr) - 1 - locate(':',secstr)); set finstr = trim(BOTH '"' FROM finstr);
return finstr;
elseif tartype = 's:' then
set fircolon = locate(':',secstr);
set finstr = substr(secstr,fircolon+1,locate(';',secstr) - fircolon);
set seccolon = locate(':',finstr);
set finstr = substr(finstr,seccolon+1,locate(';',finstr) - 1 - seccolon);
set finstr = trim(BOTH '"' FROM finstr);
return finstr;
else
set str = secstr;
end if;
end if;
else
#array
set currtar = left(tar, pointpos - 1);
set pos = locate(currtar,str);
if pos = 0 then
return null;
else
set str = substr(str,pos + LENGTH(currtar) + 2);
set tartype=left(str,2);
if tartype = 'a:' then
set tar = substr(tar, pointpos + 1);
end if;
end if;
end if;
end while;
end$$Of course, this is not true parsing , Just for the purpose of getting the value , Existing in field key The corresponding value exists in the field type to be queried (tartype Value ) There are certain conflict problems , So before using, make sure that there is no relevant string in the field value ( Of course, the possibility of conflict is relatively small ).
The whole debugging process is still very troublesome , But it pays off , Let's take a look at the implementation effect :
SELECT `name`,age,yangstr(ext,'credit') FROM my_user :

SELECT `name`,age,yangstr(ext,'title') FROM my_user :

SELECT `name`,age,yangstr(ext,'credit') FROM my_user WHERE yangstr(ext,'credit') > 100
![]()
Basically meet the use needs , If it's helpful for you to study or work , Point a free like , thank you !
边栏推荐
- Differences between Merkle DAG and Merkle tree
- ERP基础数据 华夏
- 4. fabric2.2 create and join channels (use the official demo)
- es6删除对象的某个属性
- [game theory complete information static game] Application of Nash equilibrium
- 【博弈论-完全信息静态博弈】 Nash均衡的应用
- ERP basic data Huaxia
- The way of distributed system: Lamport logical clock
- Phpexcel 10008 error resolution
- LVM management exercise
猜你喜欢

Cosmos star application case

HCIP_ MGRE experiment

Recognition of COVID-19 based on paddlepaddle

HCIP_ MGRE comprehensive experiment

Introduction to dfinity (ICP) -1

Differences between Merkle DAG and Merkle tree

ERP基础数据 金蝶

疫情之下的远程办公解决方案

Live broadcast review | bas technology innovation exploration under active defense system
![[game theory complete information static game] Nash equilibrium](/img/db/9923f5a7465c8b57182f09810b65bf.jpg)
[game theory complete information static game] Nash equilibrium
随机推荐
[virt manager] remote management the problem of the floating mouse when starting the virtual machine
中小型照明灯饰行业如何利用数字化转型突出重围?
如何通过JS动态删除table中的数据行(保留head)
Detailed explanation of digital certificate and Ca
Start from scratch - implement the jpetstore website -1- establish the project framework and project introduction
Notes on MySQL transaction not automatically submitting
LVM management exercise
EHD ether coin, the hottest dpoc mining project
Reverse order and comparison of strings
Call to undefined function think\captcha\imagettftext()
How to use annotations in word
Document contains question type
Determination of ranking
Using KVM to create three virtual machines that can communicate with local area network
MySQL summary
Homestead environment setup
ERP basic data Kingdee
How to modify desktop path in win10 system
Mysql_ Preliminary summary of database data (Continued)
About redis encapsulation tool class using distributed locks