【问题标题】:MySQL : Use JSON_ARRAY content in SELECT IN queryMySQL:在 SELECT IN 查询中使用 JSON_ARRAY 内容
【发布时间】:2020-02-04 13:29:52
【问题描述】:

我得到了以下两个表格

在'items'表中,desc列是一个JSON数组

表格项目

+--------+--------+-------+
|item_id | name   | json  |
+--------+--------+-------+
| 1      | Item 1 | [1,2] |
+--------+--------+-------+

表格说明

+----+-------------+
| id | description |
+----+-------------+
| 1  | First desc  |
+----+-------------+
| 2  | Second desc |
+----+-------------+

我想检索特定 id 的所有描述。

我试过这个,但没有运气:

SELECT id, description 
FROM descriptions 
WHERE id IN (SELECT JSON_EXTRACT(json, '$[*]') 
             FROM items 
             WHERE item_id=1)

【问题讨论】:

标签: mysql sql json


【解决方案1】:

您可以使用JSON function json_search(),它从 MySQL 5.7 开始提供:

select i.item_id, i.name, d.id, d.description
from items i
inner join descriptions d on json_search(i.json, 'one', d.id) is not null 
where i.item_id = 1

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2014-03-09
    相关资源
    最近更新 更多