gpt4 book ai didi

mysql - 更改 where 子句的顺序会破坏查询

转载 作者:行者123 更新时间:2023-12-04 13:38:05 27 4
gpt4 key购买 nike

我使用 MySQL 5.6 遇到了这种情况:

此查询有效并返回预期结果:

select *
from some_table
where a = 'b'
and metadata->>"$.country" is not null;

但是,此查询(唯一的区别是 where 子句的顺序)返回错误

select *
from some_table
where metadata->>"$.country" is not null
and a = 'b';

MySQL返回的错误是

Invalid JSON text in argument 1 to function json_extract: "Invalid value." at position 0.



为什么?

最佳答案

metdata的值列包含表格中至少一行的格式错误的 JSON。

我们希望完全删除 a = 'b'条件,我们也会观察到相同的错误。

我怀疑行为的差异是由于执行操作的顺序造成的。当a = 'b'条件首先被评估,排除 JSON_EXTRACT(metadata) 之前的行表达式被评估。由于该行与 a = 'b' 不匹配条件,MySQL 走捷径,它不评估 JSON_EXTRACT ,它已经知道该行将被排除。

当以不同的顺序进行比较时,使用 JSON_EXTRACT首先执行函数,当为 metadata 中具有无效 JSON 的行计算表达式时会引发错误.

概括:

表中至少有一行格式错误的 JSON 存储在 metadata 中。柱子。

观察到的两个查询行为的差异是由于不同的操作顺序。

建议:

考虑使用 JSON_VALID函数来识别具有无效值的行。

摘自 MySQL 引用手册
JSON_EXTRACT

Returns data from a JSON document, selected from the parts of the document matched by the path arguments. Returns NULL if any argument is NULL or no paths locate a value in the document. An error occurs if the json_doc argument is not a valid JSON document or any path argument is not a valid path expression.



https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-extract
JSON_VALID
https://dev.mysql.com/doc/refman/5.7/en/json-attribute-functions.html#function_json-valid

关于mysql - 更改 where 子句的顺序会破坏查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60660515/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com