2

我有按照这个 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           |
4

1 回答 1

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 ║
╚════╩═════════════════════╝
于 2013-04-11T12:40:57.673 回答