create table t_df(sendid int,ToID int,sendtime varchar(12),Message varchar(100));
go
insert into t_df values( 1,2,'12:30','内容1');
insert into t_df values( 2,1,'12:31','内容2');
insert into t_df values( 3,1,'12:33','内容3');
insert into t_df values( 3,1,'12:34','内容4');
insert into t_df values( 1,4,'12:41','内容5');
go
select * from t_df
where sendid in( select sendid from t_df where sendid = 1 or ToID = 1 group by sendid)
and sendtime in (select max(sendtime) from t_df where sendid = 1 or ToID = 1 group by sendid)order by sendtime
注意:t_df 为表名
查询结果:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/e7cd7b899e510fb34da6e3cbda33c895d1430c2a?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)