Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

oracle

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. How can I do that?

like image 309
Mox Avatar asked Aug 08 '16 02:08

Mox


2 Answers

Use 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.

like image 83
Tim Biegeleisen Avatar answered Nov 12 '22 06:11

Tim Biegeleisen


If you are using PostgreSQL then use below query

select * from table_name where column_name->>'key_name' = 'value_name';
like image 33
Utkarsh Avatar answered Nov 12 '22 07:11

Utkarsh