Mysql union如何将Group返回3行变成单行

时间:2014-01-08 08:30:03

标签: mysql sql select join union

Mysql union如何将3行返回到单行

 ( 
    select CONCAT(c.Email,';', c.CCEmail,';', c.AdminEmail,';', c.HREmail) as Email 
    from companies c  
    Where companyid=#companyid# 
 ) 
    union
 ( 
    select ClaimAdministratorEmail 
    from claimadminregion
    where FIND_IN_SET(#companyid#, companyid)
 )
    union
 ( 
    select LossPreventionPersonEmail 
    from losspreventionregion
    where FIND_IN_SET(#companyid#, companyid)
 )

它返回3行,但我希望它们在单行但不知道如何

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT CONCAT(c.Email,';', c.CCEmail,';', c.AdminEmail,';', c.HREmail,';', A.ClaimAdministratorEmail,';', B.LossPreventionPersonEmail) AS Email 
FROM companies c  
LEFT JOIN (SELECT #companyid# AS companyid, GROUP_CONCAT(ClaimAdministratorEmail SEPARATOR ';') ClaimAdministratorEmail 
           FROM claimadminregion WHERE FIND_IN_SET(#companyid#, companyid) 
          ) A ON c.companyid = A.companyid
LEFT JOIN (SELECT #companyid# AS companyid, GROUP_CONCAT(LossPreventionPersonEmail SEPARATOR ';') LossPreventionPersonEmail 
           FROM losspreventionregion WHERE FIND_IN_SET(#companyid#, companyid)
          ) B ON c.companyid = B.companyid
WHERE companyid=#companyid#