mysql - 带有布尔值的mysql json_extract中的奇怪行为
问题描述
在这里,我首先展示一个布尔 json_extract 的原始结果。然后我将它与真实进行比较。然后在合并后再次比较为真。
MySQL [(none)]> select json_extract('{"a": true}', '$.a');
+------------------------------------+
| json_extract('{"a": true}', '$.a') |
+------------------------------------+
| true |
+------------------------------------+
1 row in set (0.00 sec)
MySQL [(none)]> select json_extract('{"a": true}', '$.a')=true;
+-----------------------------------------+
| json_extract('{"a": true}', '$.a')=true |
+-----------------------------------------+
| 1 |
+-----------------------------------------+
1 row in set (0.01 sec)
MySQL [(none)]> select coalesce(json_extract('{"a": true}', '$.a'), 'false')=true;
+------------------------------------------------------------+
| coalesce(json_extract('{"a": true}', '$.a'), 'false')=true |
+------------------------------------------------------------+
| 0 |
+------------------------------------------------------------+
1 row in set (0.01 sec)
如您所见,合并后 MySQL 不知道我在那里处理 json 的东西。
问题:我应该如何为我的 json_extract 提供合并后备,同时能够将值与 true 进行比较?我应该使用 json 字符串select coalesce(json_extract('{"a": true}', '$.a'), 'false')='true'
吗?我不喜欢这样,因为不使用合并时相同的方法不起作用:
MySQL [(none)]> select json_extract('{"a": true}', '$.a')='true';
+-------------------------------------------+
| json_extract('{"a": true}', '$.a')='true' |
+-------------------------------------------+
| 0 |
+-------------------------------------------+
1 row in set (0.00 sec)
MySQL [(none)]> select coalesce(json_extract('{"a": true}', '$.a'), 'false')='true';
+--------------------------------------------------------------+
| coalesce(json_extract('{"a": true}', '$.a'), 'false')='true' |
+--------------------------------------------------------------+
| 1 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
如果有人查看这些查询,他们会认为这是一个错误,并且如果在未来版本的 mysql 中这得到“修补”,查询将被破坏。
实现此合并功能的正确方法是什么?
解决方案
推荐阅读
- sql-server - SSIS 设置包含字符为空值的记录
- play-billing-library - 如何解决“API 操作期间出现致命错误”(响应代码 6)
- ios - iOS11 Swift 4 的本地化问题
- java - Java Web 应用程序部署失败
- ruby - 改进 Ruby Sequel 模型代码
- css - Bootstrap中带有垂直标题的响应表
- excel - 带有时间和文本字符串的行
- amazon-web-services - AWS 目录服务不使用私有 IP 地址解析 VPC 中的资源
- c++ - 在 Qt Creator 中将 Lua 与 C++ 链接
- debugging - 调试 st.executeUpdate(sql);