我在公司的生产数据库上工作。我遇到过一种情况。这是我生成的样本数据。实际数据只是相似,只是更改了字段名。我的桌子结构和这个类似,
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我想数数学生总数(这很简单)。但是在这里,我的要求是,如果两个学生的loginDetail是相同的,那么就把他们当作一个学生。
因此,从上面的例子StudentId 333,555和777有相同的facebook电子邮件id。所以,当我计算学生的数量时,我必须考虑这两个学生的is仅为1,即使gmail帐户是不同的。所以,即使一个登录细节是相同的,对于两个人,我想把这两个人当作一个人。在生产数据中,也有这样的数据,所以我必须将4-5 personIds看作一个人,只基于他们的登录详细信息。
因此,对于上面的示例表,我需要生成查询,其中返回的学生总数为3 (而不是5)。学生身份证分别为(333,555,777),444和666
发布于 2017-04-14 13:17:00
像tis这样的查询会给出输出:
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;https://stackoverflow.com/questions/43412034
复制相似问题