I have a simple table inside a MySQL 8.0 database like this:
+-----------+---------+----------+
| id | data |created |
+-----------+---------+----------+
| INT | JSON |Timestamp |
+-----------+---------+----------+
I can populate my JSONField using:
INSERT INTO mytable (`data`) VALUES ('{
"File": {
"Files": {
"Accounts": {
"Account": [{
"AccountID": "11",
"AccountDescription": "CASH",
"Balance": "600.00"
}, {
"AccountID": "111",
"AccountDescription": "Cash",
"Balance": "600.00"
}]
}
}
}
}');
And what I want is to extract the SUM of all Balance values.
I tried this:
SELECT SUM(JSON_UNQUOTE(JSON_EXTRACT(`data`, '$.File.Files.Accounts.Account[*].Balance'))) as 'result' FROM mytable WHERE id = 1;
But gives the result:
+-----------+
| result |
+-----------+
| 0 |
+-----------+
Also, if I get rid of the SUM, the result is:
+-----------------------+
| result |
+-----------------------+
| ["600.00", "600.00"] |
+-----------------------+
Which makes me believe that JSON_UNQUOTE is not working with this nested array SELECT as well.
How can I query the table (with no custom functions preferably) so that it gives:
+-----------+
| result |
+-----------+
| 1200 |
+-----------+