MySQL内连接在一个数组字段上

时间:2011-11-09 23:21:54

标签: mysql join

我有两张桌子

[series]
--------------
ID | ART
--------------
1  | sculptor      
2  | painter
3  | writer
--------------


[artists]
--------------
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
INNER JOIN artists AS ar ON (se.id IN (ar.art_ids))

我得到的只是第一个值:

[result]
-------------------
ART      | ARTISTS
-------------------
sculptor | john
sculptor | jack
painter  | jill
writer   | jeff

而不是:

[result]
-------------------
ART      | ARTISTS
-------------------
sculptor | john
sculptor | jack
sculptor | jill
sculptor | jeff
painter  | jack
painter  | jill
writer   | jeff

通常情况下,我会使用链接p​​e.id< - > se.id的第三个表格执行此操作。但是在我的框架中维护另一个表非常复杂。

1 个答案:

答案 0 :(得分:2)

如上所述,最好的选择是修复你的表结构,最好在你有机会的时候做它。随着数据的增长,它将开始引起很多麻烦。但是如果你知道自己在做什么,我认为这会在短期内得到你想要的东西:

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