目前,JSON对象数组存储在一个名为“tax_components”的文本列中,该列位于MySQL数据库(Version7.4)中的一个名为“orders”的表中,如下所示:
第1行
[
{“tax_type”:“增值税15","tax_percentage":"15.00","tax_amount":"13.04"},
{“tax_type”:“烟草","tax_percentage":"100.00","tax_amount":"50.00"}
{“tax_type”:“豁免","tax_percentage":"0.00","tax_amount":"0.00"},
{“tax_type”:“零","tax_percentage":"0.00","tax_amount":"0.00"}
]
第2行
[
{“tax_type”:“增值税15","tax_percentage":"15.00","tax_amount":"52.17"},
{“tax_type”:“豁免","tax_percentage":"0.00","tax_amount":"0.00"},
{“tax_type”:“零","tax_percentage":"0.00","tax_amount":"0.00"}
]
我有多个行,其值类似于表中的上述内容。
我想对所有与'tax_type‘= 'Vat 15','tax_type’=‘烟草’,'tax_type‘=’豁免‘和'tax_type’=‘零’的税额值做同样的处理。
上面的JSON数组在不同的行中有不同的值。有时它有tax_type =‘烟草’,有时它没有。
请有人告诉我要写什么SQL查询来提取和求和与这个'tax_amount‘列中不同的'tax_type’对应的所有'tax_components‘值?
提前谢谢。
发布于 2022-06-25 12:24:05
作品:(改编自@Barmar的另一个答案)
SELECT
SUM(
CAST(
(
JSON_EXTRACT (
`tax_components`,
JSON_UNQUOTE(
REPLACE(
JSON_SEARCH(`tax_components`, 'one', 'Vat 15'),
'tax_type',
'tax_amount'
)
)
)
) AS DECIMAL(10, 2)
)
)
FROM
ordershttps://stackoverflow.com/questions/72753608
复制相似问题