首页 > 解决方案 > 如何在 MySQL 中的字符串中提取字符串

问题描述

我有一个字符串值:

'{"CUSTOMER_TOPUP":{"bankname":"45","amount":"1000","depositreference":"REF1111"}}'

并想返回值:REF1111

这是我的 Json 字符串的最后一部分。

请注意,我的字符串可以有不同的值。例如:

'{"BILLPAYMENT":{"utility":"11","amount":"50","accountnumber":"123456"}}' 

我需要返回值字符串 123456

我在创建自己的函数时严格使用 Mysql。

标签: mysqlstringsubstringlocate

解决方案


为什么不使用 JSON 函数?

json_extract(
    '{"CUSTOMER_TOPUP":{"bankname":"45","amount":"1000","depositreference":"REF1111"}}',
    '$.CUSTOMER_TOPUP.depositreference'
)

或者,如果您想去掉结果值周围的双引号:

json_unquote(json_extract(
    '{"CUSTOMER_TOPUP":{"bankname":"45","amount":"1000","depositreference":"REF1111"}}',
    '$.CUSTOMER_TOPUP.depositreference'
))

推荐阅读