Postgresql json like query

孤者浪人 提交于 2019-12-03 02:06:50

If the data column is text type, then use ->> on cast:

select * from module_data where data::json->>'title' like '%Board%'

If it's already json:

select * from module_data where data->>'title' like '%Board%'

One other option which may be sufficient for other people who've found this page is to just cast the column to text type. Eg

select * from module_data where data::text like '%Board%'

Note though, this will search over the entire json and should only be used if you can guarantee the other fields won't be a problem.

I found the following is more straight-forward and easier for jsonb type of columns:

select * from table_name
where 
column_name::text like '%Something%'

Found a good article on more examples and implementations: https://www.compose.com/articles/faster-operations-with-the-jsonb-data-type-in-postgresql/

Hope it helps!

I Think it should be like

select * from module_data where data->>'$."title"' like '%Board%'

then only it worked for me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!