-1

我有一个类似于以下的表:

CREATE TABLE `foo` (
  `foo_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `data` json DEFAULT NULL,
  `data_type` int(11) NOT NULL,
  `data_status` int(11) NOT NULL,
  PRIMARY KEY (`foo_id`)
);

插入一些数据

insert into foo(data, data_type, data_status) values ('{\"id\": \"FOO\", \"code\": \"FOO\"}', 1, 1);
insert into foo(data, data_type, data_status) values ('{\"id\": \"BAR\", \"code\": \"BAR\"}', 1, 1);
insert into foo(data, data_type, data_status) values ('{\"id\": \"BAZ\", \"code\": \"BAZ\"}', 1, 1);

显示表格内容:

mysql> select * from foo;
+--------+------------------------------+-----------+-------------+
| foo_id | data                         | data_type | data_status |
+--------+------------------------------+-----------+-------------+
|      1 | {"id": "FOO", "code": "FOO"} |         1 |           1 |
|      2 | {"id": "BAR", "code": "BAR"} |         1 |           1 |
|      3 | {"id": "BAZ", "code": "BAZ"} |         1 |           1 |
+--------+------------------------------+-----------+-------------+
3 rows in set (0.01 sec)

此查询有效:

select f.data, f.data_type, f.data_status
from foo f
where (JSON_EXTRACT(f.data, '$.code') in ('FOO'))
   and (f.data_type in (1)) 
   and (f.data_status in (1));

+------------------------------+-----------+-------------+
| data                         | data_type | data_status |
+------------------------------+-----------+-------------+
| {"id": "FOO", "code": "FOO"} |         1 |           1 |
+------------------------------+-----------+-------------+
1 row in set (0.00 sec)

这个也是:

select f.data, f.data_type, f.data_status
from foo f
where (JSON_EXTRACT(f.data, '$.code') in ('BAR'))
   and (f.data_type in (1)) 
   and (f.data_status in (1));

+------------------------------+-----------+-------------+
| data                         | data_type | data_status |
+------------------------------+-----------+-------------+
| {"id": "BAR", "code": "BAR"} |         1 |           1 |
+------------------------------+-----------+-------------+

不过这个不...

select f.data, f.data_type, f.data_status
from foo f
where (JSON_EXTRACT(f.data, '$.code') in ('FOO','BAR'))
   and (f.data_type in (1)) 
   and (f.data_status in (1));

Empty set, 1 warning (0.00 sec)

如何使第三个查询起作用?

4

1 回答 1

2

我注意到最后一个查询有警告

Empty set, 1 warning (0.00 sec)

...

mysql> SHOW WARNINGS;
+---------+------+-----------------------------------------------------------------------------------+
| Level   | Code | Message                                                                           |
+---------+------+-----------------------------------------------------------------------------------+
| Warning | 1235 | This version of MySQL doesn't yet support 'comparison of JSON in the IN operator' |
+---------+------+-----------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

然而,这个警告让我看到了这篇文章

https://bugs.mysql.com/bug.php?id=93800

因此,通过修改查询以用引号将 IN 参数括起来,它可以工作

select f.data, f.data_type, f.data_status 
from foo f 
where (JSON_EXTRACT(f.data, '$.code') in ('"FOO"','"BAR"'))    
and (f.data_type in (1))     
and (f.data_status in (1));
+------------------------------+-----------+-------------+
| data                         | data_type | data_status |
+------------------------------+-----------+-------------+
| {"id": "FOO", "code": "FOO"} |         1 |           1 |
| {"id": "BAR", "code": "BAR"} |         1 |           1 |
+------------------------------+-----------+-------------+
2 rows in set, 1 warning (0.00 sec)

虽然仍然得到同样的警告......

该解决方案仅适用于多个 IN 参数,如果您在最初成功的前两个查询上尝试它会中断

mysql> select f.data, f.data_type, f.data_status 
from foo f 
where (JSON_EXTRACT(f.data, '$.code') in ('"BAR"'))    
and (f.data_type in (1))     
and (f.data_status in (1));
Empty set (0.00 sec)
于 2020-04-01T02:36:46.317 回答