版权声明:转载请留言告知,注明出处 https://blog.csdn.net/qq_36213352/article/details/83060378
1.如果数据量小的话,将json数据直接复制到mysql的json字段中,如果数据过大可以通过java等后台形式对json数据解析,然后写入数据库中。
查询操作
select *,json->'$.features[0].geometry.rings' as rings from JSON;
从一张表读取一部分数据存入另一张表中(一条数据)
insert into DT_village(name, border) SELECT
json->'$.features[0].attributes.CJQYMC',json->'$.features[0].geometry.rings'
from JSON;
读取json数据并写入数据库(此时使用的是定义函数的形式来执行方法,可以定义便量)
#清空数据库
TRUNCATE table DT_village;
#定义存储过程
delimiter //
DROP PROCEDURE IF EXISTS insert_test_val;
##num_limit 要插入数据的数量,rand_limit 最大随机的数值
CREATE PROCEDURE insert_test_val()
BEGIN
DECLARE i int default 0;
DECLARE a,b varchar(5000);
WHILE i<10 do
set a=CONCAT('$.features[',i,'].attributes.CJQYMC');
set b=CONCAT('$.features[',i,'].geometry.rings');
insert into DT_village(name, border) select
#json->'$.features[0].attributes.CJQYMC',json->'$.features[0].geometry.rings'
# (json->a),(json->b)
json_extract(json,a),json_extract(json,b)
from JSON;
set i = i + 1;
END WHILE;
END
//
#调用存储过程
call insert_test_val();
调用游标的方式获取jsosn数据中的一行,并执行插入操作
delimiter //
drop procedure if exists StatisticStore;
CREATE PROCEDURE StatisticStore()
BEGIN
#创建接收游标数据的变量
declare j json;#存储json数据
DECLARE i int default 0; #创建总数变量,记录执行次数,控制循环
DECLARE a,b,c varchar(5000);#定义json数组中的某个数据的键值
#创建结束标志变量
declare done int default false;
#创建游标
declare cur cursor for select json from JSON where name = '1';
#指定游标循环结束时的返回值
declare continue HANDLER for not found set done = true;
#设置初始值
set a=CONCAT('$.features[',i,'].attributes.XZQDM');
set b=CONCAT('$.features[',i,'].attributes.XZQMC');
set c=CONCAT('$.features[',i,']');
#打开游标
open cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch cur into j;
#判断游标的循环是否结束
if done then
leave read_loop;#跳出游标循环
end if;
#这里可以做任意你想做的操作
WHILE i<11 do
insert into dt_border(xzq_code,name,border) select
json_extract(j,a),json_extract(j,b),json_extract(j,c)
from JSON;
set i = i + 1;
END WHILE;
#结束游标循环
end loop;
#关闭游标
close cur;
#输出结果
select j,i;
END;
#调用存储过程
call StatisticStore();