mysql 遍历json数据结构,在mysql函数中循环遍历JSON对象

I have a json object which has list of products under a bill. I want to write a mysql function for it which reads the data from the json and iterates over it one by one and inserts the same data to product and bill tables.

Here is my json object

{"billNo":16,"date":"2017-13-11 09:05:01","customerName":"Vikas","total":350.0,"fixedCharges":100,"taxAmount":25.78,"status":paid,"product":[{"productId":"MRR11","categoryId":72,"categoryName":"Parker Pen","cost":200,"quantity":2,"log":{"supplierId":"725","supplierName":"Rihant General Stores"}},{"productId":"MRR12","categoryId":56,"categoryName":"Drawing Books","cost":150,"quantity":3,"log":{"supplierId":"725","supplierName":"Rihant General Stores"}}]}

Here I have a mysql function which reads the data from the JSON

CREATE DEFINER=`mydb`@`%` FUNCTION `raiseOrder`(dataObject Json)

RETURNS bigint(11)

BEGIN

DECLARE billNo BIGINT(11) DEFAULT NULL;

DECLARE customerName VARCHAR(64);

DECLARE date datetime DEFAULT NOW();

DECLARE total Float(12,2);

DECLARE taxamt Float(12,2);

DECLARE fixedCharges Float(12,2);

DECLARE products json;

DECLARE productId bigint(15) DEFAULT NULL;

DECLARE categoryId bigint(11);

DECLARE cost float;

DECLARE categoryName varchar(64);

DECLARE quantity int default 0;

DECLARE supplierId bigint(11);

DECLARE supplierName varchar(128);

SET billNo = (SELECT JSON_EXTRACT(dataObject, "$.billNo"));

SET customerName = (SELECT JSON_EXTRACT(dataObject, "$.customerName"));

SET products = (SELECT JSON_EXTRACT(dataObject, "$.products"));

SET productId = (SELECT JSON_EXTRACT(products, "$[0].productId"));

RETURN 1;

END

Now with these lines

SET products = (SELECT JSON_EXTRACT(dataObject, "$.products"));

SET productId = (SELECT JSON_EXTRACT(products, "$[0].productId"));

I get the inner products json and the id of the 0th product. But I want a way to iterate over the array of the products.

解决方案

You can use a WHILE loop in conjunction with JSON_LENGTH to achieve this:

DECLARE json, products, product VARCHAR(4000);

DECLARE i INT DEFAULT 0;

SELECT '{"billNo":16,"date":"2017-13-11 09:05:01","customerName":"Vikas","total":350.0,"fixedCharges":100,"taxAmount":25.78,"status":"paid","product":[{"productId":"MRR11","categoryId":72,"categoryName":"Parker Pen","cost":200,"quantity":2,"log":{"supplierId":"725","supplierName":"Rihant General Stores"}},{"productId":"MRR12","categoryId":56,"categoryName":"Drawing Books","cost":150,"quantity":3,"log":{"supplierId":"725","supplierName":"Rihant General Stores"}}]}

' INTO json;

SELECT json->"$.product" INTO products;

WHILE i < JSON_LENGTH(products) DO

SELECT JSON_EXTRACT(products,CONCAT('$[',i,']')) INTO product;

SELECT product;

SELECT i + 1 INTO i;

END WHILE;

You'll probably need to do more than simply 'SELECT product' though ;-)

NOTE: MySQL JSON functions were added in 5.7.8 so you'll need to check your MySQL version first.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值