【问题标题】:I want to retrieve SUM using key of JSON field in MySQL我想在 MySQL 中使用 JSON 字段的键检索 SUM
【发布时间】:2021-06-09 21:18:20
【问题描述】:

我想使用 MySQL json_extractjson_object 或任何其他从 JSON 类型数据字段中获取 vehicle_rentmaterial_amountother_amountlabour_amountpa_amountta_amount 的总和jsonMySQL的相关方法。

{
    "task_vehicle": [
        {
            "vehicle_note": "Anonymous Proof",
            "vehicle_rent": "2000"
        }
    ],
    "task_material": [
        {
            "material_note": "Anonymous Proof",
            "material_amount": 200
        }
    ],
    "task_regular_amount": {
        "other": {
            "other_amount": "250"
        },
        "labour": {
            "labour_amount": "250"
        }
    },
    "task_purchase_breakdown": [
        {
            "pa_amount": "200"
        },
        {
            "pa_amount": "200"
        }
    ],
    "task_transport_breakdown": [
        {
            "ta_amount": "250",
            "transport_type": "Bus",
        }
    ]
}

如果 json 数据如下所示,我可以提取和求和这些值-

{
"vehicle_rent": "2000",
"material_amount": "200",
"other_amount": "250".
"ta_amount": "250"
}

【问题讨论】:

    标签: mysql mysql-json


    【解决方案1】:

    不能影响订单。

    SELECT JSON_OBJECT('vehicle_rent',JSON_EXTRACT('{
        "task_vehicle": [
            {
                "vehicle_note": "Anonymous Proof",
                "vehicle_rent": "2000"
            }
        ],
        "task_material": [
            {
                "material_note": "Anonymous Proof",
                "material_amount": 200
            }
        ],
        "task_regular_amount": {
            "other": {
                "other_amount": "250"
            },
            "labour": {
                "labour_amount": "250"
            }
        },
        "task_purchase_breakdown": [
            {
                "pa_amount": "200"
            },
            {
                "pa_amount": "200"
            }
        ],
        "task_transport_breakdown": [
            {
                "ta_amount": "250",
                "transport_type": "Bus"
            }
        ]
    }'
    , '$.task_vehicle[0].vehicle_rent'),'material_amount',
    JSON_EXTRACT('{
        "task_vehicle": [
            {
                "vehicle_note": "Anonymous Proof",
                "vehicle_rent": "2000"
            }
        ],
        "task_material": [
            {
                "material_note": "Anonymous Proof",
                "material_amount": 200
            }
        ],
        "task_regular_amount": {
            "other": {
                "other_amount": "250"
            },
            "labour": {
                "labour_amount": "250"
            }
        },
        "task_purchase_breakdown": [
            {
                "pa_amount": "200"
            },
            {
                "pa_amount": "200"
            }
        ],
        "task_transport_breakdown": [
            {
                "ta_amount": "250",
                "transport_type": "Bus"
            }
        ]
    }'
    , '$.task_material[0].material_amount')
    ,'other_amount',
    JSON_EXTRACT('{
        "task_vehicle": [
            {
                "vehicle_note": "Anonymous Proof",
                "vehicle_rent": "2000"
            }
        ],
        "task_material": [
            {
                "material_note": "Anonymous Proof",
                "material_amount": 200
            }
        ],
        "task_regular_amount": {
            "other": {
                "other_amount": "250"
            },
            "labour": {
                "labour_amount": "250"
            }
        },
        "task_purchase_breakdown": [
            {
                "pa_amount": "200"
            },
            {
                "pa_amount": "200"
            }
        ],
        "task_transport_breakdown": [
            {
                "ta_amount": "250",
                "transport_type": "Bus"
            }
        ]
    }'
    , '$.task_regular_amount.other.other_amount'),'ta_amount',
    JSON_EXTRACT('{
        "task_vehicle": [
            {
                "vehicle_note": "Anonymous Proof",
                "vehicle_rent": "2000"
            }
        ],
        "task_material": [
            {
                "material_note": "Anonymous Proof",
                "material_amount": 200
            }
        ],
        "task_regular_amount": {
            "other": {
                "other_amount": "250"
            },
            "labour": {
                "labour_amount": "250"
            }
        },
        "task_purchase_breakdown": [
            {
                "pa_amount": "200"
            },
            {
                "pa_amount": "200"
            }
        ],
        "task_transport_breakdown": [
            {
                "ta_amount": "250",
                "transport_type": "Bus"
            }
        ]
    }'
    , '$.task_transport_breakdown[0].ta_amount')) as result
    
    |结果 | | :------------------------------------------------ ------------------------------------------ | | {“ta_amount”:“250”,“other_amount”:“250”,“vehicle_rent”:“2000”,“material_amount”:200} |

    db小提琴here

    【讨论】:

    • 感谢您的帮助。它对我理解查询和思考方式有很大帮助。喜欢它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多