数组字段

时间:2017-04-19 09:38:24

标签: mysql

我有2张桌子

[系列]

--------------
ID | ART
--------------
1  | sculptor      
2  | painter
3  | writer
-----------

[艺术家]

--------------
NAME | ART_IDs
--------------
john | 1
jack | 1,2
jill | 2,1
jeff | 3,1

我希望像这样加入:

SELECT se.art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0

我得到的只是第一个值:

[结果]

-------------------
ART      | ARTISTS
-------------------
sculptor | john
sculptor | jack
painter  | jill
writer   | jeff

而不是:

[结果]

----------------------------
ART               | ARTISTS
----------------------------
sculptor          | john
sculptor,painter  | jack
painter,sculptor  | jill
writer,sculptor   | jeff

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT GROUP_CONCAT(se.art ORDER BY FIND_IN_SET(se.id , ar.art_ids)) as art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0
GROUP BY ar.name

如果您想在每个组中连续列,GROUP_CONCAT可以帮助您。

在Rextester中查看demo