如何在另一个表B中加入mysql复选框表A和复选框结果字段?

时间:2013-04-11 12:25:50

标签: mysql sql

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

1 个答案:

答案 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 ║
╚════╩═════════════════════╝