Get records where json column key is null

前端 未结 2 1015
名媛妹妹
名媛妹妹 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:38

    You need to use the ->> operator for that:

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

    Because the -> operator returns 'null'::json (and not sql NULL) if the key is exists but with a json null value.

    http://sqlfiddle.com/#!15/76ec4/2

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题