计算json数组里面的值(转)

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      |
+-----------+

mysqlmysql-8.0mysql-json

Share

Follow

asked Oct 24, 2018 at 11:39

正在上传…重新上传取消

drec4s

7,75388 gold badges2929 silver badges4949 bronze badges

Add a comment

1 Answer

Sorted by:

                                              Highest score (default)                                                                   Trending (recent votes count more)                                                                   Date modified (newest first)                                                                   Date created (oldest first)                              

4

In order to get the desired result, you can make use of JSON Table Functions in MySQL 8.0. It converts JSON data into tabular form.Then onward you can use aggregate function on result.

The query to achieve the same is given below

SELECT sum(result) as result
  FROM mytable,
   JSON_TABLE(
    `data`,
     '$.File.Files.Accounts.Account[*]' COLUMNS(           
          NESTED PATH '$.Balance' COLUMNS (result DECIMAL PATH '$')           
    )
) AS jt;

The DB fiddle link is given below
DB Fiddle - SQL Database Playground

More information on JSON Table Functions can be found below
MySQL :: MySQL 8.0 Reference Manual :: 12.18.6 JSON Table Functions

Share

Follow

SELECT sum(result) as result
  FROM test,
   JSON_TABLE(
    `id`,
     '$.cccc[*]' COLUMNS(           
          NESTED PATH '$.age' COLUMNS (result DECIMAL PATH '$')           
    )
) AS jt;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值