Below follow my scenario:
CREATE TABLE `CustomerOrder` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`data` json DEFAULT NULL,
PRIMARY KEY (`id`)
);
And we can use this Customer Order json as example:
{
"creation": "2015-07-30 14:27:51",
"customer": {
"id": 2,
"email": "[email protected]"
},
"item": [
{
"sku": 182,
"unitPrice": 0.89,
"qty": 10
}, {
"sku": 712,
"unitPrice": 12.99,
"qty": 2
}
]
}
Runing on MySQL console this SQL:
SELECT json_extract(data, '$.item[*].unitPrice') AS price FROM CustomerOrder
;
I will have this output:
[ 0.89, 12.99 ]
Now how can I evaluate a SUM of [0.89 + 12.99] or 1..N elements of items?
For my tests I used this version of MySQL Labs:
http://mysqlserverteam.com/json-labs-release-native-json-data-type-and-binary-format/