json mysql 判断值是否存在_检查mysql中的json编码数组中是否存在值

I am working on a Mysql trigger to do something on my database. Before any thing, I want to check if a value exists in a json_encode string in my config table and after that proceed to run the trigger.

my config table is handled by php scripts and looks like this:

-------------------------------------------

| config_name | config_value |

-------------------------------------------

| target_id | ["1","16","18","22","37"] |

-------------------------------------------

in php we can use:

if in_array(json_decode(config_value)) and ...

but my problem is in Mysql syntax which doesn't support in_array and json_decode

How can I check if my value exists in 'config value' in Mysql trigger?

解决方案

How to solve this problem

If you are storing JSON in mysql, make sure that you upgrade to mysql 5.7, then you can make use of the range of JSON functions available. In your particular case, you can do

SELECT * FROM my_table WHERE JSON_SEARCH(config_value,"one", "17") IS NOT NULL;

What you Definitely ought to be doing

You have a problem in your data. If you find that you are always searching a JSON field, what that really means is that your table should be normalized.

update: section 2, title changed as suggested by @Sammitch

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值