0

I have a table ,one of the table filed is json format:

my_table

id  json_field

1   { "to_status": 7, "to_status_name": "In Progress", "role": "admin"}

2   { "to_status": 3, "to_status_name": "Completed", "role": "admin"}

3   { "to_status": 2, "to_status_name": "Completed", "role": "customer"}

How can I select all rows that "to_status" is 3 or 2 ?

Any friend can help?

2 Answers 2

2

You can use SELECT * FROM my_table WHERE JSON_EXTRACT(json_field, '$.to_status') IN (3, 2)

or

SELECT * FROM my_table WHERE json_field->>'$.to_status' IN (3, 2)

to select what you need.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply ,I have tired both of them ,none of them work for my case.
Try this one too. SELECT * FROM my_table WHERE json_field->>'to_status' = '3' OR json_field->>'to_status' = '2'
1

When extracted Json values are strings. So you need to test for string values or cast to_status extracted values to an integer:

SELECT * FROM my_table WHERE json_field->>'to_status' in ('2','3');
or
SELECT * FROM my_table WHERE (json_field->>'to_status')::int in (2,3);

But then you can get the same results extracting the to_status_name:

SELECT * FROM my_table WHERE json_field->>'to_status_name' = 'Completed';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.