如何解决如何对MySQL JSON列属性进行空检查?
我正在使用MySQL 8.0.21。我需要编写一个与JSON列类型一起使用的查询。 JSON文档中的某些数据具有空值,我想过滤掉这些空值。
示例中可能存在的行,为简单起见,已删除了JSON文档中的大多数属性:
jsonColumn
'{"value":96.0}'
'{"value":null}' -- This is the row I am trying to filter out
NULL
这是我尝试过的:
-- Removed columns where jsonColumn was NULL but,NOT columns where jsonColumn->'$.value' was null.
SELECT *
FROM <table>
WHERE jsonColumn->'$.value' IS NOT NULL;
-- Note the unquote syntax,->>. The code above uses ->.
-- Produced the same result as the code above.
SELECT *
FROM <table>
WHERE jsonColumn->>'$.value' IS NOT NULL;
-- Produced same result as the two above. Not surprised because -> is an alias of JSON_EXTRACT
SELECT *
FROM <table>
WHERE JSON_EXTRACT(jsonColumn,'$.value') IS NOT NULL;
-- Produced same result as the three above. Not surprised because ->> is an alias of JSON_EXTRACT
SELECT *
FROM <table>
WHERE JSON_UNQUOTE(JSON_EXTRACT(jsonColumn,'$.value')) IS NOT NULL;
-- Didn't really expect this to work. It didn't work. For some reason it filters out all records from the select.
SELECT *
FROM <table>
WHERE jsonColumn->'$.value' != NULL;
-- Unquote syntax again. Produced the same result as the code above.
SELECT *
FROM <table>
WHERE jsonColumn->>'$.value' != NULL;
-- Didn't expect this to work. Filters out all records from the select.
SELECT *
FROM <table>
WHERE JSON_EXTRACT(jsonColumn,'$.value') != NULL;
-- Didn't expect this to work. Filters out all records from the select.
SELECT *
FROM <table>
WHERE JSON_UNQUOTE(JSON_EXTRACT(jsonColumn,'$.value')) != NULL;
-- I also tried adding a boolean value to one of the JSON documents,'{"test":true}'. These queries did not select the record with this JSON document.
SELECT *
FROM <table>
WHERE jsonColumn->'$.test' IS TRUE;
SELECT *
FROM <table>
WHERE jsonColumn->>'$.test' IS TRUE;
我注意到了一些有趣的事情...
比较其他值起作用。例如...
-- This query seems to work fine. It filters out all records except those where jsonColumn.value is 96.
SELECT *
FROM <table>
WHERE jsonColumn->'$.value' = 96;
在上面的一些示例的注释中提到的另一件有趣的事情是对null检查的奇怪行为。 如果jsonColumn为null,即使知道我正在访问jsonColumn->'$。value',null检查也会过滤掉记录。
不确定这是否清楚,所以让我详细说明一下...
-- WHERE jsonColumn->>'$.value' IS NOT NULL
jsonColumn
'{"value":96.0}'
'{"value":null}' -- This is the row I am trying to filter out. It does NOT get filtered out.
NULL -- This row does get filtered out.
根据this post,将->>以及JSON_UNQUOTE和JSON_EXTRACT与IS NOT NULL比较,应该可以正常工作。我以为那是可以的。
老实说,这可能是IS语句和JSON列类型的错误。将其与JSON文档而不是JSON文档的值进行比较时已经存在一些奇怪的行为。
无论如何,有什么方法可以做到这一点?还是我一直在尝试的方法被证实是正确的方法,而这仅仅是一个错误?
解决方法
在Barmar发表评论之后...
显然,此更改在8.0.13之前的某个时候发生了。 forums.mysql.com/read.php?176,670072
论坛中的解决方法似乎使用JSON_TYPE。看起来是一个糟糕的解决方法。
SET @doc = JSON_OBJECT('a',NULL);
SELECT JSON_UNQUOTE(IF(JSON_TYPE(JSON_EXTRACT(@doc,'$.a')) = 'NULL',NULL,JSON_EXTRACT(@doc,'$.a'))) as C1,JSON_UNQUOTE(JSON_EXTRACT(@doc,'$.b')) as C2;
论坛帖子上说(关于变通方法之前发布的代码)...
C2实际上设置为NULL,但是C1作为4个字符的“ null”字符串返回。
所以我开始搞乱字符串比较...
// This filtered out NULL jsonColumn but,NOT NULL jsonColumn->'$.value'
SELECT *
FROM <table>
WHERE jsonColumn->'$.value' != 'null';
jsonColumn
'{"value":96.0}'
'{"value":"null"}' -- Not originally apart of my dataset but,this does get filtered out. Which is very interesting...
'{"value":null}' -- This does NOT get filtered out.
NULL -- This row does get filtered out.
// This filtered out both NULL jsonColumn AND NULL jsonColumn->'$.value'
SELECT *
FROM <table>
WHERE jsonColumn->>'$.value' != 'null';
jsonColumn
'{"value":96.0}'
'{"value":"null"}' -- Not originally apart of my dataset but,this does get filtered out.
'{"value":null}' -- This does get filtered out.
NULL -- This row does get filtered out.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。