首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除字符&和数字值

删除字符&和数字值
EN

Database Administration用户
提问于 2018-10-25 17:51:41
回答 1查看 213关注 0票数 0

我使用MySQL5.6,并希望从db列的json输出中删除所有字符,并在此之后删除所需的数值和

输入:

{"across_back":"0",“熙熙攘攘”:“0”,"waiscvvt":"50","hisdp":"50","neckdf":"0","thiddgh":"0"} {“肩”:“0”,“bustd”:“10”,"waisdsdt":"40","sdhip":"10","nsdfeck":"0","thigfsdh":"0"} {“后退”:“0”,“胸”:“40”,“腰部”:“35”,“助跑”:“22”,“颈”:“10”,“大腿”:“0”}

输出:

代码语言:javascript
复制
0+0+0+50+0+0 = 50
0+10+40+10+0+0 = 60
0+40+35+22+10+0 107
EN

回答 1

Database Administration用户

回答已采纳

发布于 2018-10-26 09:15:37

代码语言:javascript
复制
drop table if exists test;
create table if not exists test(val varchar(255));

drop table if exists log;
create table log(id int auto_increment primary key,
                 val varchar(255),
                 summ int );

insert into test
values
('{"across_back":"0","bustd":"0","waiscvvt":"50","hisdp":"50","neckdf":"0","thiddgh":"0"} '),
('{"shoulder":"0","bustfd":"10","waisdsdt":"40","sdhip":"10","nsdfeck":"0","thigfsdh":"0"} '),
('{"back":"0","bust":"40","waist":"35","sdfhip":"22","necsdk":"10","thigh":"0"}');

drop function if exists my_sum;

DELIMITER @@;

create function my_sum(s text)
returns bigint deterministic
begin
set @sum := 0;
while locate('"', s) do
-- debug 1, remove after debugging
    insert into log(val, summ) select s, @sum;
    set @sum := @sum + substring_index(substring_index(s, '"', 4), '"', -1);
-- debug 2, remove after debugging
    insert into log(val, summ) select s, @sum;
    set s = substring(s FROM 2 + length(substring_index(s, '"', 4)));
-- debug 3, remove after debugging
    insert into log(val, summ) select s, @sum;
end while;
return @sum;
end;
@@;

DELIMITER ;

select val, my_sum(val) from test;

drop table if exists test;
drop function if exists my_sum;

select * from log order by id;
drop table if exists log;
票数 1
EN
页面原文内容由Database Administration提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://dba.stackexchange.com/questions/221018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档