2

I have mysql 'text' column with data as json which looks like below, how to query using specific value, where Banking is model name , info is field name

Banking 
  info : text

info: {"age": 23, name: "John"}
3

2 Answers 2

1

In my opinion you can try search by LIKE query as below but because your database save data by text so it is very difficult to handle exception case. So I think you should try use json type of mysql or split it into table and save age as integer.

select * from bankings where info LIKE '%"age": 23%'

Ruby

Banking.where('infor LIKE ?', '%"age": 23%')
Sign up to request clarification or add additional context in comments.

Comments

0

You should use ActiveRecord to perform this query using ->> operator:

Banking.where("info ->> 'name' = 'John'")

3 Comments

It did not worked , given syntax error, tried the same which you had suggested @matanco
Incorrect syntax near '>'.: EXEC sp_executesql N'SELECT [bankings].* FROM [bankings] WHERE (info ->> ''account_type'' = ''card'')'
could you please share reference about this operator '->> ' in Activerecord guides or any reference

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.