我有这个sqlFiddle的表: http://www.sqlfiddle.com/#!2/df04a/1
Onepiece table:
| ID | Checkbox_IDs |
| 1 | 1,2 |
| 2 | 3 |
checkbox
| ID | Name |
| 1 | Luffy |
| 2 | Zorro |
请帮我查询一下,我想要“onepiece”表中的所有行,而不是显示“1,2”或“2,3,4”的checkbox_id列,它应该显示数字链接到的在复选框表上:
Onepiece results table:
| ID | Checkbox_IDs |
| 1 | Luffy, Zorro |
| 2 | Sanji |
答案 0 :(得分:2)
SELECT a.ID,
GROUP_CONCAT(b.Name) Names
FROM OnePiece a
INNER JOIN CheckBoxes b
ON FIND_IN_SET(b.ID, a.Checkbox_ID) > 0
GROUP BY a.ID
输出
╔════╦═════════════════════╗
║ ID ║ NAMES ║
╠════╬═════════════════════╣
║ 7 ║ Luffy,Zorro ║
║ 8 ║ Sanji ║
║ 9 ║ Sanji,Chopper,Zorro ║
╚════╩═════════════════════╝