GROUP_CONCAT 不应包含其当前值

GROUP_CONCAT should not include its current value

当前查询我正在尝试通过父 UiniqId 获取兄弟记录的名称

SELECT 
    `SubApplicants`.`SubAppId` AS `AppId`,
    `SubApplicants`.`SubApplicationId` AS `ApplicationId`,
    `SubApplicants`.`SubFirstName`,
    `SubApplicants`.`SubSurname`,
    GROUP_CONCAT(CONCAT(`SubApplicants`.`SubFirstName`,
                ' ',
                `SubApplicants`.`SubSurname`)
        SEPARATOR ', ') AS `SubCoApplicantsNames`
   
FROM
    `SubApplicants`
WHERE
    (`SubApplicants`.`IsSubGuarantor` = 0 )
GROUP BY  `SubApplicants`.`SubApplicationId`

问题不准确,因为您显示的输出(“当前结果”)未按 ApplicationId 分组,正如@forpas 也注意到的那样,因此它并不代表您的真实情况。但是,如果我对您的理解正确,您可以尝试此操作,因为代码将为您提供所需的输出。

SELECT Applicants.Id AS Id, 
    Applicants.ApplicationId AS ApplicationId, 
    Applicants.FirstName, 
    Applicants.Surname, 
    GROUP_CONCAT(CONCAT (
            Applicants2.FirstName, 
            ' ', 
            Applicants2.Surname
            ) SEPARATOR ', ') AS CoApplicantsNames
FROM Applicants
INNER JOIN Applicants AS Applicants2 ON Applicants.ApplicationId = Applicants2.ApplicationID
WHERE Applicants.FirstName != Applicants2.FirstName
    OR Applicants.Surname != Applicants2.Surname
GROUP BY Applicants2.ApplicationId;

第一个FROM会给你所有可能的申请和应聘者的欲望。您还可以根据需要对其进行过滤(例如 (Applicants.IsGuarantor = 0 ) 。加入的 table 将为您提供共同申请人的最后一个组连接字段。