MySQL Join two tables with comma separated values MySQL Join two tables with comma separated values mysql mysql

MySQL Join two tables with comma separated values


SELECT  a.nid,        GROUP_CONCAT(b.name ORDER BY b.id) DepartmentNameFROM    Notes a        INNER JOIN Positions b            ON FIND_IN_SET(b.id, a.forDepts) > 0GROUP   BY a.nid


Table 1╔══════════╦═════════════════╗║ nid      ║    forDepts     ║╠══════════╬═════════════════╣║ 11,2,4           ║║ 24,5             ║╚══════════╩═════════════════╝Table 2╔══════════╦═════════════════╗║ id       ║    name         ║╠══════════╬═════════════════╣║ 1        ║ Executive       ║║ 2        ║ Corp Admin      ║║ 3        ║ Sales           ║║ 4        ║ Art             ║║ 5        ║ Marketing       ║╚══════════╩═════════════════╝SELECT * FROM table1 as t1 LEFT JOIN table2 as t2 ON find_in_set(t2.id, t1.forDepts)Output  ╠══════════╬════════════════════════════╣ ║ 1        ║ Executive, Corp Admin, Art ║ ║ 2        ║ Art, Marketing             ║ ╚══════════╩════════════════════════════╝