计算 table 中的组,但如果 2 组中的任何记录相同,则仅将其视为一个组

Count groups in table but if any record in 2 group is same then consider it as one group only

我正在组织中的生产数据库上工作。我遇到过一种情况。这是我生成的示例数据。实际数据只是相似,只是更改了字段名称。 我的table结构和这个类似,

tableAi - unsigned bigint(20) - Auto increment index
loginDetails -  varchar(200)     
loginType - tinyint(4)  (2- gmail, 3 facebook)   
studentid - unsigned bigint(20)  

tableAi | loginDetails | loginType | studentId
1 | abc@gmail.com | 2 | 333
2 | abc@facebook.com | 3 | 333
3 | xyz@facebook.com | 3 | 444
4 | xxx@gmail.com | 2 | 444
5 | test@gmail.com | 2 | 555
6 | abc@facebook.com | 3 | 555
7 | ac@facebook.com | 3 | 666
8 | ac@gmail.com | 2 | 777
9 | abc@facebook.com | 3 | 777

我想统计学生总数(很简单)。但是这里我的要求是,如果 2 个学生的 loginDetail 相同,则将他们视为一个学生。

因此,从上面的示例 StudentId 333、555 和 777 具有相同的 facebook 电子邮件 ID。因此,当我计算学生人数时,即使 gmail 帐户不同,我也只能将这 2 个学生 ID 视为 1。因此,即使一个登录详细信息相同,对于 2 个人,我也只想将这 2 个人视为 1 个人。在生产数据中,还有这样的数据,我必须仅根据他们的登录详细信息将 4-5 个 personIds 视为一个人。

因此,对于上述示例 table,我需要生成 returns 学生总数为 3 的查询。(不是 5)。 不同的学生 ID 将是 (333,555,777) 、 444 和 666

像这样的一些查询会给你输出:

SELECT 
 count(*), -- only for test
 GROUP_COUNCAT(t.studentId) AS stundents,
 t.loginDetails,
 MAX(t.loginType) as loginType,
 t.studentId
FROM tableAi t
GROUP BY t.loginDetails
HAVING count(*) = 2;