Get records where json column key is null

前端 未结 2 1024
名媛妹妹
名媛妹妹 2020-12-10 00:58

I have table users with json column details.

I want to fetch all user records where details[\"email\"] is null or email key doesn\'t exist.

2条回答
  •  执念已碎
    2020-12-10 01:39

    use brackets (). Looks like compiler tries to see it like details->('email' IS NOT NULL). So you can fix it like this:

    select *
    from users
    where (details->'email') is not null
    

    sql fiddle demo

    actually, to get records where details["email"] is null or email key doesn't exist, you can use this query:

    select *
    from users
    where (details->>'email') is null
    

    as described in this answer.

提交回复
热议问题