mysql选择列表名称加入find_in_set列表id

问题描述 投票:0回答:1

列出与其他表连接的名称

t1 
id  orderid items
1   1001    1,2,5
2   1002    5,3
3   1003    2,4
t2 
id   name
1    item1
2    item2
3    item3
4    item4
5    item5

需要使用 join 、GROUP_CONCAT 和 FIND_IN_SET 来生成 select 语句,因为订单表有 58k

orderid  itemNames 
1001     item1,item2,item5
1002     item5,item3 
1003     item2,item4
select v.orderid,sub.itemNames from t1 as v 
LEFT JOIN 
(select id,GROUP_CONCAT(DISTINCT name) as itemnames from t2) 
 AS sub  ON FIND_IN_SET(sub.id,v.items ) 

不工作

mysql group-concat find-in-set
1个回答
0
投票
SELECT t1.orderid, GROUP_CONCAT(t2.name) itemNames
FROM t1
JOIN t2 ON FIND_IN_SET(t2.id, t1.items)
GROUP BY 1
© www.soinside.com 2019 - 2024. All rights reserved.