Manoj Menon
Manoj Menon

Reputation: 1038

How to Query Mysql text column

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"}

Upvotes: 2

Views: 566

Answers (2)

Bình Trương
Bình Trương

Reputation: 400

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%')

Upvotes: 1

matanco
matanco

Reputation: 2129

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

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

Upvotes: 0

Related Questions