我在MYSQL数据库中有一列想要提取该列数据并将其拆分成多列这里是我要分割的数据样本
```
this.FormGroup.updateValueAndValidity()
正如您所注意到的那样,这些数据有不同的长度,并且希望能够分割任意长度的数据,看看这里[How to split a resulting column in multiple columns 但我不认为这就是我想要的结果,因为也希望修剪数据上的所有其他括号和引号。到目前为止,这是我的代码 ```
{"1744":"1","1745":"1","1747":"1","1748":"1","1749":"1","1750":"1"}
{"1759":"1"}
{"47":"1","48":"Ehebr","49":"1479977045596.jpg"}
```
答案 0 :(得分:0)
如果你可以使用程序和游标
drop procedure if exists p;
delimiter //
CREATE DEFINER=`root`@`localhost` PROCEDURE `p`(
IN `instring` varchar(255)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
declare tempstring varchar(10000);
declare outstring varchar(100);
declare c1 varchar(100);
declare c2 varchar(100);
declare checkit int;
declare done int;
DECLARE CUR1 CURSOR for SELECT t.col FROM T;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
drop table if exists occursresults;
create table occursresults (col1 varchar(20), col2 varchar(20));
open CUR1;
read_loop: LOOP
FETCH CUR1 INTO tempstring;
if done then leave read_loop; end if;
set tempstring = replace(tempstring,'{','');
set tempstring = replace(tempstring,'}','');
set tempstring = replace(tempstring,'"','');
set checkit = 0;
#select tempstring;
looper: while tempstring is not null and instr(tempstring,',') > 0 do
set checkit = checkit + 1;
if checkit > 100 then #In case of infinite loop
leave looper;
end if;
set outstring = substr(tempstring,1,instr(tempstring, ',') - 1);
set tempstring = ltrim(rtrim(replace(tempstring,concat(outstring,','),'')));
set c1 = substr(outstring,1,instr(outstring, ':') - 1);
set c2 = replace(outstring,concat(c1,':'),'');
INSERT INTO OCCURSRESULTS (COL1,COL2) VALUES (c1,c2);
# select tempstring,outstring,c1,c2;
end while;
#select tempstring;
set outstring = tempstring;
set c1 = substr(outstring,1,instr(outstring, ':') - 1);
set c2 = replace(outstring,concat(c1,':'),'');
INSERT INTO OCCURSRESULTS (Col1,Col2) VALUES (c1,c2);
end loop;
close cur1;
end //
delimiter ;
MariaDB [sandbox]> select * from t;
+---------------------------------------------------------------------+
| col |
+---------------------------------------------------------------------+
| {"1744":"1","1745":"1","1747":"1","1748":"1","1749":"1","1750":"1"} |
| {"1759":"1"} |
| {"47":"1","48":"Ehebr","49":"1479977045596.jpg"} |
+---------------------------------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> call p(1);
Query OK, 0 rows affected (0.65 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> SELECT * FROM OCCURSRESULTS;
+------+-------------------+
| col1 | col2 |
+------+-------------------+
| 1744 | 1 |
| 1745 | 1 |
| 1747 | 1 |
| 1748 | 1 |
| 1749 | 1 |
| 1750 | 1 |
| 1759 | 1 |
| 47 | 1 |
| 48 | Ehebr |
| 49 | 1479977045596.jpg |
+------+-------------------+
10 rows in set (0.00 sec)