如何查询MySQL JSON列中的null值?

huangapple go评论76阅读模式
英文:

How to query MySQL JSON column data for null values?

问题

我在MySQL数据库的JSON类型列中存储JSON数据。尝试过滤extra_data列,其中payment_address不为null。不幸的是找不到正确的方法。

我尝试过的查询如下:

SELECT * FROM orders WHERE extra_data->"$.payment_address" != NULL;
SELECT * FROM orders WHERE extra_data->"$.payment_address" IS NOT NULL;
SELECT * FROM orders WHERE extra_data->"$.payment_address" != "null";

对于extra_data列,JSON数据如下:

extra_data
{"tracking_number": "", "payment_amount": null, "payment_address": null}
{"tracking_number": "", "payment_amount": null, "payment_address": "testaddress"}

如何查询JSON列中的某些字段是否为null/非null值?

英文:

I'm storing JSON data in JSON type column in MySQL database. Tried to filter extra_data column for where payment_address is not null. Unfortunately couldn't find the right way.

These are the queries I tried:

SELECT * FROM orders WHERE extra_data->"$.payment_address" != NULL;
SELECT * FROM orders WHERE extra_data->"$.payment_address" IS NOT NULL;
SELECT * FROM orders WHERE extra_data->"$.payment_address" != "null";

For the extra_data column, JSON data is like this:

extra_data
{"tracking_number": "", "payment_amount": null, "payment_address": null}
{"tracking_number": "", "payment_amount": null, "payment_address": "testaddress"}

How can I query some field in JSON column for null/not null values?

答案1

得分: 2

这可能与 bug 85755 有关,与 JSON 的 null 和 SQL 的 NULL 之间的差异有关。无论如何,您可以通过使用 ->>JSON_UNQUOTE 来解决这个问题:

SELECT *
FROM orders
WHERE extra_data->>"$.payment_address" != 'null';

SELECT *
FROM orders
WHERE JSON_UNQUOTE(extra_data->"$.payment_address") != 'null';

在这两种情况下的输出是:

extra_data
{"payment_amount": null, "payment_address": "testaddress", "tracking_number": ""}

在 db-fiddle 上的演示

英文:

This is likely related to bug 85755 which relates to the difference between JSON null and SQL NULL. Anyway, you can workaround this by using ->> or JSON_UNQUOTE instead:

SELECT *
FROM orders
WHERE extra_data->>"$.payment_address" != 'null';

SELECT *
FROM orders
WHERE JSON_UNQUOTE(extra_data->"$.payment_address") != 'null';

In both cases the output is:

extra_data
{"payment_amount": null, "payment_address": "testaddress", "tracking_number": ""}

Demo on db-fiddle

答案2

得分: 1

You could use REPLACE to replace 'null' value into empty value, then get only not empty values:

使用 REPLACE 将 'null' 值替换为空值,然后仅获取非空值:

SELECT extra_data
FROM orders
WHERE REPLACE(JSON_EXTRACT(extra_data, '$.payment_address'), 'null', '') <> '';

Result:

结果:

{"payment_amount": null, "payment_address": "testaddress", "tracking_number": ""}

在此查看演示

英文:

You could use REPLACE to replace 'null' value into empty value, then get only not empty values :

SELECT extra_data
FROM orders
where REPLACE(JSON_EXTRACT(extra_data, &#39;$.payment_address&#39;), &#39;null&#39;, &#39;&#39;)  &lt;&gt; &#39;&#39; ;

Result :

{&quot;payment_amount&quot;: null, &quot;payment_address&quot;: &quot;testaddress&quot;, &quot;tracking_number&quot;: &quot;&quot;}

Demo here

huangapple
  • 本文由 发表于 2023年5月11日 17:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定