查询存储在数据库字段中的String中的特定值

时间:2016-09-19 07:42:10

标签: sql postgresql

{"create_channel":"1","update_comm":"1","channels":"*"}

这是我要查询的数据库字段。

如果我想选择所有包含"create_channel": "1""update_comm": "1"

的记录,我的查询会是什么样子?

其他问题:

查看以下字段:

{"create_channel":"0","update_comm":"0","channels":[{"ch_id":"33","news":"1","parties":"1","questions ":"1","cam":"1","edit":"1","view_subs":"1","invite_subs":"1"},{"ch_id":"18","news":"1","parties":"1","questions ":"1","cam":"1","edit":"1","view_subs":"1","invite_subs":"1"}]}

我如何查找NewspartiesquestionsCams部分中的所有子请点

1 个答案:

答案 0 :(得分:1)

您可以使用the ->> operator将成员作为字符串返回:

select  * 
from    YourTable 
where   YourColumn->>'create_channel' = '1' and
        YourColumn->>'update_comm' = '1'

要查找在第33频道中有新闻,参与方,问题和广告的用户,您可以使用the @> operator检查频道数组是否包含这些属性:

select  *
from    YourTable
where   YourColumn->'channels' @> '[{
            "ch_id":"33",
            "news":"1",
            "parties":"1",
            "questions ":"1",
            "cam":"1"
        }]';