有點遲來的答案,但我只是遇到了這個問題,找不到任何合理的文件。我結束使用的解決方案是上面指出的'abl'的json_type函數。
訣竅是與字符串'NULL'不爲空或NULL進行比較。
作爲測試引發以下到一個MySQL提示和玩的值
(如果使用phpMyAdmin不要忘記「在此再次顯示此查詢」檢查和「保留查詢框」 - 宇宙令人沮喪,不會丟失編輯..)
set @a='{"a":3,"b":null}';
select if(json_type(json_extract(@a,'$.b')) = 'NULL',1,0);
我結束了以下。
mysql> set @a='{"a":3,"b":null}';
Query OK, 0 rows affected (0.00 sec)
mysql> select if(json_type(json_extract(@a,'$.b')) = 'NULL',1,0);
+----------------------------------------------------+
| if(json_type(json_extract(@a,'$.b')) = 'NULL',1,0) |
+----------------------------------------------------+
| 1 |
+----------------------------------------------------+
1 row in set (0.00 sec)
mysql> set @a='{"a":3,"b":1}';
Query OK, 0 rows affected (0.00 sec)
mysql> select if(json_type(json_extract(@a,'$.b')) = 'NULL',1,0);
+----------------------------------------------------+
| if(json_type(json_extract(@a,'$.b')) = 'NULL',1,0) |
+----------------------------------------------------+
| 0 |
+----------------------------------------------------+
1 row in set (0.00 sec)
作爲存儲過程的裸骨 - 這正是我所需要的 - 使用'if'語句而不是if()函數。
drop procedure if exists test;
delimiter $$
create procedure test(in x json)
begin
if json_type(json_extract(x,'$.b')) = 'NULL' then
select 1;
else
select 0;
end if;
end$$
delimiter;
mysql> call test('{"a":3,"b":1}');
+---+
| 0 |
+---+
| 0 |
+---+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> call test('{"a":3,"b":null}');
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)