mySQL从不同的表中加入数据

时间:2013-09-19 14:49:26

标签: mysql join concat

好吧,我从mySQl数据库中提取用户列表,然后我在另一个表中查找组分配。如果将它们分配给多个组,则会按原样返回多行。

我的问题是如何将群组名称连结到结果的一列。

  SELECT `u`.`ID`
         ,CONCAT(`First-Name`," ",`Last-Name`) as `Name`
         ,`g`.`Group-Name`
    FROM `application-users` AS `u`
    JOIN `groups` AS `g` ON (`g`.`Assigned-Users` LIKE CONCAT("%|",`u`.`ID`,"|%") )
   WHERE `u`.`Status` = "Active" && `u`.`Type` = "Business Development" 
ORDER BY `First-Name` ASC

提前致谢!

1 个答案:

答案 0 :(得分:0)

SELECT `u`.`ID`,CONCAT(`First-Name`," ",`Last-Name`) as `Name`,
GROUP_CONCAT(`g`.`Group-Name` SEPARATOR ', ') as `Groups`,
CONCAT('|',GROUP_CONCAT(`g`.`ID` SEPARATOR '|'),'|') as `GroupIDs`
FROM `application-users` AS `u`
JOIN `groups` AS `g` ON (`g`.`Assigned-Users` LIKE CONCAT("%|",`u`.`ID`,"|%"))
WHERE `u`.`Status` = "Active" && `u`.`Type` = "Business Development" 
GROUP BY `u`.`ID`

感谢您的时间偷看!