结合结果MYSQL

时间:2013-10-21 07:02:45

标签: php mysql

我想将几个结果合并到1个表中以便在PHP中显示,坏的是没有唯一的密钥来识别它,这是我的mysql表数据: -

ServiceGroup    Name      Moperator          ShortCode  
--------------  ---------  -----------------  ---------  
INTEGRAT        norman     KW_DTAC            11111             
KW_I2C          norman     KW_ZAIN            00000            
AB_MK           norman     HUT/TRUEMOVE       4541311           
INTERGRAT       amanda     KW_DTAC            11111      

在列出的表中,名称amanda和norman具有相同的值,但我希望结果在PHP上显示如下。

ServiceGroup    Name              Moperator          ShortCode  
--------------  ---------         -----------  ---------  
INTEGRAT        norman,amanda     KW_DTAC            11111             
KW_I2C          norman            KW_ZAIN            00000            
AB_MK           norman            HUT/TRUEMOVE       4541311    

我应该如何将它放在PHP上以及查询应该是什么样的?这是我的查询示例: -

SELECT Distinct userfilters.ServiceGroup, userdetails.Login, userfilters.Moperator, userfilters.ShortCode FROM userdetails INNER JOIN userfilters ON userfilters.UserDetailsID=userdetails.UserDetailsID AND Login IN ('amanda', 'brandon', 'bryan', 'christina', 'johnny', 'larry', 'monica', 'norman', 'eko') ORDER BY ServiceGroup DESC

1 个答案:

答案 0 :(得分:0)

按唯一列对数据进行分组,并使用grop_concat将登录信息放在逗号分隔列表中

SELECT f.ServiceGroup, 
       group_concat(d.Login) as logins,
       f.Moperator, 
       f.ShortCode 
FROM userdetails d
INNER JOIN userfilters f ON f.UserDetailsID = d.UserDetailsID 
WHERE d.Login IN ('amanda', 'brandon', 'bryan', 'christina', 'johnny', 'larry', 'monica', 'norman', 'eko') 
GROUP by f.ServiceGroup, f.Moperator, f.ShortCode
ORDER BY f.ServiceGroup DESC