如何在mysql中包含json的where子句?

时间:2016-12-29 23:27:21

标签: mysql mysql-5.7

例如,我在表A中有一个名为json的列

Json列包含这样的json数据:

Int

我想使用包含dept_code = 012

的json列获取数据记录
record 1 : {"dept_code": "012", "unit_code": "22"}
record 2 : {"dept_code": "013", "unit_code": "23"}
etc

我该怎么做?

注意:

我试图在网站stackoverflow上找到答案,但我没有找到它。所以我提出这个问题

3 个答案:

答案 0 :(得分:4)

也许这会有所帮助:

SELECT * FROM table_A where json like '%"dept_code": "012"%';

答案 1 :(得分:1)

非常确定JSON_CONTAINS就是你想要的。 View the docs here

我现在没有沙盒来测试这个,但是你的例子会转换为:

select * from table_A
where json_contains(json, '012', '$.dept_code')

因为dept_code是存储在json列中的对象的属性。

答案 2 :(得分:0)

 ...WHERE JSON_CONTAINS(`json `, '{"dept_code " : "012"}')