select 用户名,电子邮件,时间 from tb t where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间) order by 时间 desc
SELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE 用户名=t.用户名 and 电子邮件<>t.电子邮件) order by 时间 DESC
谢谢楼上的这些回答,我的意思再说明点!!我现在有一个表中现在想根据电子邮件不重复的查出些用户的 用户名,电子邮件,最后登录时间,最后是根据这个最后登录时间进行倒排序我用 select distinct Email from Person where PerFlag_1=0 查出是115971条记录 那我想用以楼上这些方法 查出来的记录 也应该是115971条的吧
select * from 表名 where Email in(select Email from 表名 group by Email having count(Email)=1) order by 时间 desc
select * from 表名 where Email in(select Email from 表名 group by 用户名 having count(Email)=1) order by 时间 desc
select 用户名,最后登录时间,电子邮件 from 表名 where 电子邮件 in (select distinct 电子邮件 from 表名) order by 最后登录时间 desc
select 用户名,最后登录时间,电子邮件 from 表名 where 电子邮件 in (select distinct 电子邮件 from 表名 where datediff(year,时间字段名,getDate()=0) order by 最后登录时间 desc datediff(year,时间字段名,getDate()=0表示去年的时间
这个方法还有点问题!因为PerFlag_1!=0的记录里也有可能包含上面select distinct Email from Person where PerFlag_1=0查出来的电子邮件,所以数字上会出现问题!
select 用户名,电子邮件,时间 from tb t where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间) and PerFlag_1=0 order by 时间 desclz给个数据,然后给个要求结果吧。这样说很难完全理解。
from tb t
where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间)
order by 时间 desc
select distinct Email from Person where PerFlag_1=0
查出是115971条记录
那我想用以楼上这些方法 查出来的记录 也应该是115971条的吧
select 用户名,最后登录时间,电子邮件
from 表名
where 电子邮件 in (select distinct 电子邮件 from 表名)
order by 最后登录时间 desc
select 用户名,最后登录时间,电子邮件
from 表名
where 电子邮件 in
(select distinct 电子邮件 from 表名 where datediff(year,时间字段名,getDate()=0)
order by 最后登录时间 desc
datediff(year,时间字段名,getDate()=0表示去年的时间
select 用户名,电子邮件,时间
from tb t
where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间) and PerFlag_1=0
order by 时间 desclz给个数据,然后给个要求结果吧。这样说很难完全理解。