How to sum values within a field with Arrays in JSON with MYSQL?
I have a table with a field that sometimes has multiple arrays in JSON.
MYSQL 8.0.11
DLL
CREATE TABLE T
(`user` varchar(4), `sales` int, `reference` varchar(400) DEFAULT NULL, `quantity` float DEFAULT NULL, `orders` int)
;
Data
INSERT INTO T
(`user`, `sales`, `reference`, `quantity`, `orders`)
VALUES
('xx01', 100, '[{"productid":"80000052","quantity":"1","reference":"ML-41"},{"quantity":1,"reference":"ML-32","productid":"ML-52"},{"productid":"80000052","quantity":3,"reference":"ML-11"}]', 0, 0),
('xx02', 200, '[{"productid":"80000052","quantity":2}]', 0, 0),
('xx02', 400, '[{"productid":"80000052","quantity":3}]', 0, 0),
('xx03', 300, '[{"productid":"80000052","quantity":4}]', 0, 0),
('xx03', 500, '[{"productid":"80000052","quantity":5}]', 0, 0)
;
The following query updates field "quantity" with the value of "quantity" in the "reference" field JSON array(s):
UPDATE T t2,
( SELECT sales, quant FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
) t1
SET t2.quantity = t1.quant
WHERE t1.sales = t2.sales;
The query takes the "quantity" in the first array of the first row and ignores the following 2 arrays in that row's "reference" field.
Is there a way to sum the "quantity" of all 3 arrays of "reference" field of the first row?
mysql
add a comment |
I have a table with a field that sometimes has multiple arrays in JSON.
MYSQL 8.0.11
DLL
CREATE TABLE T
(`user` varchar(4), `sales` int, `reference` varchar(400) DEFAULT NULL, `quantity` float DEFAULT NULL, `orders` int)
;
Data
INSERT INTO T
(`user`, `sales`, `reference`, `quantity`, `orders`)
VALUES
('xx01', 100, '[{"productid":"80000052","quantity":"1","reference":"ML-41"},{"quantity":1,"reference":"ML-32","productid":"ML-52"},{"productid":"80000052","quantity":3,"reference":"ML-11"}]', 0, 0),
('xx02', 200, '[{"productid":"80000052","quantity":2}]', 0, 0),
('xx02', 400, '[{"productid":"80000052","quantity":3}]', 0, 0),
('xx03', 300, '[{"productid":"80000052","quantity":4}]', 0, 0),
('xx03', 500, '[{"productid":"80000052","quantity":5}]', 0, 0)
;
The following query updates field "quantity" with the value of "quantity" in the "reference" field JSON array(s):
UPDATE T t2,
( SELECT sales, quant FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
) t1
SET t2.quantity = t1.quant
WHERE t1.sales = t2.sales;
The query takes the "quantity" in the first array of the first row and ignores the following 2 arrays in that row's "reference" field.
Is there a way to sum the "quantity" of all 3 arrays of "reference" field of the first row?
mysql
add a comment |
I have a table with a field that sometimes has multiple arrays in JSON.
MYSQL 8.0.11
DLL
CREATE TABLE T
(`user` varchar(4), `sales` int, `reference` varchar(400) DEFAULT NULL, `quantity` float DEFAULT NULL, `orders` int)
;
Data
INSERT INTO T
(`user`, `sales`, `reference`, `quantity`, `orders`)
VALUES
('xx01', 100, '[{"productid":"80000052","quantity":"1","reference":"ML-41"},{"quantity":1,"reference":"ML-32","productid":"ML-52"},{"productid":"80000052","quantity":3,"reference":"ML-11"}]', 0, 0),
('xx02', 200, '[{"productid":"80000052","quantity":2}]', 0, 0),
('xx02', 400, '[{"productid":"80000052","quantity":3}]', 0, 0),
('xx03', 300, '[{"productid":"80000052","quantity":4}]', 0, 0),
('xx03', 500, '[{"productid":"80000052","quantity":5}]', 0, 0)
;
The following query updates field "quantity" with the value of "quantity" in the "reference" field JSON array(s):
UPDATE T t2,
( SELECT sales, quant FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
) t1
SET t2.quantity = t1.quant
WHERE t1.sales = t2.sales;
The query takes the "quantity" in the first array of the first row and ignores the following 2 arrays in that row's "reference" field.
Is there a way to sum the "quantity" of all 3 arrays of "reference" field of the first row?
mysql
I have a table with a field that sometimes has multiple arrays in JSON.
MYSQL 8.0.11
DLL
CREATE TABLE T
(`user` varchar(4), `sales` int, `reference` varchar(400) DEFAULT NULL, `quantity` float DEFAULT NULL, `orders` int)
;
Data
INSERT INTO T
(`user`, `sales`, `reference`, `quantity`, `orders`)
VALUES
('xx01', 100, '[{"productid":"80000052","quantity":"1","reference":"ML-41"},{"quantity":1,"reference":"ML-32","productid":"ML-52"},{"productid":"80000052","quantity":3,"reference":"ML-11"}]', 0, 0),
('xx02', 200, '[{"productid":"80000052","quantity":2}]', 0, 0),
('xx02', 400, '[{"productid":"80000052","quantity":3}]', 0, 0),
('xx03', 300, '[{"productid":"80000052","quantity":4}]', 0, 0),
('xx03', 500, '[{"productid":"80000052","quantity":5}]', 0, 0)
;
The following query updates field "quantity" with the value of "quantity" in the "reference" field JSON array(s):
UPDATE T t2,
( SELECT sales, quant FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
) t1
SET t2.quantity = t1.quant
WHERE t1.sales = t2.sales;
The query takes the "quantity" in the first array of the first row and ignores the following 2 arrays in that row's "reference" field.
Is there a way to sum the "quantity" of all 3 arrays of "reference" field of the first row?
mysql
mysql
edited Nov 13 '18 at 23:19
BaliJack
asked Nov 13 '18 at 8:46
BaliJackBaliJack
4917
4917
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of WHERE, use JOIN ... ON plus GROUP BY and SUM() AS
UPDATE T t2
JOIN (SELECT sales, SUM(quant) AS qu FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
GROUP BY sales) t1
ON t2.sales = t1.sales
SET t2.quantity = t1.qu;
db-fiddle
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277025%2fhow-to-sum-values-within-a-field-with-arrays-in-json-with-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of WHERE, use JOIN ... ON plus GROUP BY and SUM() AS
UPDATE T t2
JOIN (SELECT sales, SUM(quant) AS qu FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
GROUP BY sales) t1
ON t2.sales = t1.sales
SET t2.quantity = t1.qu;
db-fiddle
add a comment |
Instead of WHERE, use JOIN ... ON plus GROUP BY and SUM() AS
UPDATE T t2
JOIN (SELECT sales, SUM(quant) AS qu FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
GROUP BY sales) t1
ON t2.sales = t1.sales
SET t2.quantity = t1.qu;
db-fiddle
add a comment |
Instead of WHERE, use JOIN ... ON plus GROUP BY and SUM() AS
UPDATE T t2
JOIN (SELECT sales, SUM(quant) AS qu FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
GROUP BY sales) t1
ON t2.sales = t1.sales
SET t2.quantity = t1.qu;
db-fiddle
Instead of WHERE, use JOIN ... ON plus GROUP BY and SUM() AS
UPDATE T t2
JOIN (SELECT sales, SUM(quant) AS qu FROM T, JSON_TABLE(T.reference,
"$[*]" COLUMNS(
quant VARCHAR(400) PATH "$.quantity"
)
) AS jt1
GROUP BY sales) t1
ON t2.sales = t1.sales
SET t2.quantity = t1.qu;
db-fiddle
answered Nov 14 '18 at 1:16
BaliJackBaliJack
4917
4917
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277025%2fhow-to-sum-values-within-a-field-with-arrays-in-json-with-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown