首页 > 解决方案 > MySQL Query select concat on json field ids

问题描述

我有这两张桌子

表 1:“身份”

ID | identifier | report_ids
------------------------------
1  | TOP        | ["1","2"]

“report_ids”是一个 VARCHAR 列,而不是 JSON

表 2:“报告”

ID | name
------------------------------
1  | Report Food
2  | Report Beverage

我必须从表 Identity 中选择并加入表 Reports 以通过他的 id 获取名称

总之,我希望通过查询得到这个结果:

Result:
ID | identifier | report_names
--------------------------------
1  | TOP        | Report Food, Report Beverage

我怎么能用 Query 做到这一点?

在此先感谢:)

标签: mysqljsonjoinconcatenation

解决方案


寻找

SELECT i.id, i.identifier, GROUP_CONCAT(r.name)
FROM Identity i
JOIN Reports r ON LOCATE(CONCAT('"', r.id, '"'), i.report_ids)
GROUP BY i.id, i.identifier
-- WHERE JSON_VALID(i.report_ids)

推荐阅读