Skip to content

Instantly share code, notes, and snippets.

@danlynn
Last active June 12, 2020 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danlynn/5de1d0c83c39d9ba5f0464eaace49b97 to your computer and use it in GitHub Desktop.
Save danlynn/5de1d0c83c39d9ba5f0464eaace49b97 to your computer and use it in GitHub Desktop.
mysql json queries
select properties->>"$.cow" from ahoy_events where properties->>"$.cow" is not null;
-- Finds rows where cow attr is not missing - BUT not where cow attr is not null
# null
# 1
# 2
# 3
# null
select properties->>"$.cow" from ahoy_events where properties->>"$.cow" != CAST('null' AS JSON);
-- Finds rows where cow attr is not missing - BUT not where cow attr is not null
# null
# 1
# 2
# 3
# null
select properties->>"$.cow" from ahoy_events where JSON_EXTRACT(`properties`, '$.cow') != CAST('null' AS JSON);
-- Finds rows where value of cow is not missing and not null
# 1
# 2
# 3
select JSON_EXTRACT(`properties`, '$.cow') from ahoy_events where JSON_EXTRACT(`properties`, '$.cow') != CAST('null' AS JSON);
-- Finds rows where value of cow is not missing and not null
# 1
# 2
# 3
select properties->>"$.cow" from ahoy_events where properties->>"$.cow" != 'null';
-- Finds rows where value of cow is not missing and not null
# 1
# 2
# 3
select properties->>"$.cow" from ahoy_events where properties is not null;
-- Finds all rows where properties is not null - displays just the cow value
-- Note that the <null> values indicate that cow attr was not found
# <null>
# ...
# <null>
# null
# 1
# 2
# 3
# null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment