我有以下使用成员资格表和用户表的查询。我喜欢做的是对于有计数(*)>的电子邮件。 1,我也想显示UserName。
SELECT au.UserName, am.Email
FROM [dbo].[aspnet_Membership] am
join aspnet_Users au
on am.UserId = au.UserId
group by am.Email
having count(*) > 1
order by email
不确定怎么做。我尝试使用max(au.UserName),但这只会给我第一个用户名而不是全部用户名
答案 0 :(得分:1)
只需将au.UserName添加到group by子句中即可。
group by au.UserName, am.Email
答案 1 :(得分:1)
使用count
窗口函数。
select userName,Email
from (SELECT au.UserName,am.Email, count(*) over(partition by am.Email) as cnt_per_email
FROM [dbo].[aspnet_Membership] am
join aspnet_Users au on am.UserId = au.UserId
) t
where cnt_per_email>1
答案 2 :(得分:0)
您可以将IN
与子查询一起使用。
SELECT
au.UserName,
am.Email
FROM
[dbo].[aspnet_Membership] am
join aspnet_Users au
on am.UserId = au.UserId
where
am.Email in (select Email from [aspnet_Membership] group by Email having count(*) > 1)