Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get records where json column key is null

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.

This doesn't work:

SELECT users.* FROM users where details->'email' IS NOT NULL;

ERROR: operator does not exist: json -> boolean

like image 869
morgoth Avatar asked May 28 '14 12:05

morgoth


1 Answers

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.

like image 130
Roman Pekar Avatar answered Sep 23 '22 19:09

Roman Pekar