gpt4 book ai didi

MySQL 通过 json 列交集进行搜索

转载 作者:行者123 更新时间:2023-11-29 10:03:42 25 4
gpt4 key购买 nike

MySQL 5.7.22

我有一个名为 products 的表,其中包含一个 json 列shipping_method_ids我想选择与至少一个 Shipping_method_id 匹配的所有产品,我该怎么做?

目前,我有以下内容:

mysql> DESCRIBE products;
+----------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+----------------+
| shipping_method_ids | json | YES | | NULL | |
+----------------------+---------------+------+-----+---------+----------------+


SELECT * FROM products WHERE(JSON_CONTAINS products.shipping_method_ids, [1, 2, 3])

因此,我希望获得与至少一个 Shipping_method_id 匹配的所有产品,但我不知道使用什么作为条件,例如

mysql> SELECT id, shipping_method_ids FROM products WHERE (...);
+----+---------------------+
| id | shipping_method_ids |
+----+---------------------+
| 1 | [1, 2, 3] |
| 2 | [1] |
| 3 | [2, 3] |
+----+---------------------+

如何通过给定数组的当前交集选择所有产品?

最佳答案

我认为这会起作用

SELECT * 
FROM products
WHERE
JSON_CONTAINS(shipping_method_ids,'1') OR
JSON_CONTAINS(shipping_method_ids,'2') OR
JSON_CONTAINS(shipping_method_ids,'3')

或者你可以使用正则表达式

SELECT * FROM products WHERE shipping_method_ids REGEXP '[1-3]'

关于MySQL 通过 json 列交集进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52404196/

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