Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过mysql函数中的JSON对象进行循环_Mysql_Json - Fatal编程技术网

通过mysql函数中的JSON对象进行循环

通过mysql函数中的JSON对象进行循环,mysql,json,Mysql,Json,我有一个json对象,它有一个账单下的产品列表。我想为它编写一个mysql函数,它从json中读取数据并逐个迭代,然后将相同的数据插入product和bill表 这是我的json对象 {"billNo":16,"date":"2017-13-11 09:05:01","customerName":"Vikas","total":350.0,"fixedCharges":100,"taxAmount":25.78,"status":paid,"product":[{"productId":"MRR

我有一个json对象,它有一个账单下的产品列表。我想为它编写一个mysql函数,它从json中读取数据并逐个迭代,然后将相同的数据插入product和bill表

这是我的json对象

{"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"}}]}
这里我有一个mysql函数,它从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
现在用这些台词

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

我得到内部产品json和第0个产品的id。但是我想要一种迭代产品数组的方法

您可以将WHILE循环与JSON_LENGTH结合使用来实现这一点:

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;
声明json、产品、产品VARCHAR(4000);
声明i INT默认值为0;
选择“{”billNo“:16,“date”:“2017-13-11 09:05:01”,“customerName”:“Vikas”,“total”:350.0,“fixedCharges”:100,“taxAmount”:25.78,“status”:“paid”,“productId:”MRR11“,”categoryId:”72,“categoryName:”Parker Pen“,”cost:”200,“数量:”,2,“log:“{”supplierId:”725,“supplierName:“Rihant General Stores”},{”MRR12“,”categoryId:”56,“类别名称”:“图纸簿”,“成本”:150,“数量”:3,“日志”:{“供应商ID”:“725”,“供应商名称”:“Rihant General Stores”}}}
'转换为json;
选择json->“$.product”进入产品;
而我的长度(产品)是
选择JSON_EXTRACT(products,CONCAT(“$[”,i,“]])到product中;
选择产品;
选择i+1进入i;
结束时;
您可能需要做的不仅仅是简单地“选择产品”;-)


注意:MySQL JSON函数是在5.7.8中添加的,因此您需要首先检查您的MySQL版本。

我不会在MySQL函数中这样做。我会用Python或Ruby编写一个脚本来完成。除了MySQL存储例程之外,其他语言都支持更方便的JSON处理方式。但我必须在MySQL中这样做。目前,MySQLQL用于JSON处理的函数非常有限,因此建议使用另一种语言,但是,可以实现的第一种近似方法是,根据需要调整脚本。