简体   繁体   English

如何在WHERE子句中使用json列作为条件

[英]How to use json column in the WHERE clause as a condition

The question is stated in the title and below is an example of the data问题在标题中说明,下面是数据示例

insert into table A values('a','b', {'key':'value'});

And I would like to be able to select this row based on the key-value pair using the WHERE clause.我希望能够使用 WHERE 子句基于键值对 select 这一行。 How can I do that?我怎样才能做到这一点?

Use JSON_VALUE : 使用JSON_VALUE

SELECT t.*
FROM tableA t
WHERE JSON_VALUE(col3, '$.key') LIKE 'some_value'

This assumes that the column which contains the JSON value {'key':'value'} is called col3 . 这假设包含JSON值{'key':'value'}的列称为col3

If you are using PostgreSQL then use below query如果您使用的是 PostgreSQL,请使用以下查询

select * from table_name where column_name->>'key_name' = 'value_name';

Here is solution that's work for me: 这是适用于我的解决方案:

column_name->'$.key'

Tested with mysql server 5.7.27 用mysql server 5.7.27测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM