我在 mysql 查询时遇到问题。我想要获得共享相同tenant_group_id 编号的唯一名称对。您需要知道的是,每个租户都有一个唯一的 individual_tenant_id,并且最多两个租户共享相同的tenant_group_id。
SELECT t1.first_name, t2.first_name
FROM individualtenant t1
LEFT JOIN individualtenant t2 ON t1.tenant_group_id = t2.tenant_group_id
AND t1.individual_tenant_id != t2.individual_tenant_id
现在这给了我配对,但双向,例如,它会返回“John”,“Melissa”和“Melissa”,“John”,但我只需要一个独特的配对。
编辑:如果个人租户没有合作伙伴,我需要在第二列中设置 NULL。
最佳答案
尝试:
SELECT t1.first_name, t2.first_name
FROM individualtenant t1
LEFT JOIN individualtenant t2
ON t1.tenant_group_id = t2.tenant_group_id
WHERE t1.individual_tenant_id < t2.individual_tenant_id
or t2.individual_tenant_id is null;
关于sql - MySQL 选择唯一的名称配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3419234/