sql使用自连接去重复查询

查询公司与公司最新刷新的一条信息

    select  t1.userid,t1.id,t1.title,t1.RegType,t1.Salary,t1.SubjectID,t1.RefreshTime,t2.name,t2.logo,t2.[address],t2.CreateTime 

    from info t1 inner join   

    companyinfo t2 on t1.userid=t2.id

    where t1.id in 

    (

        select top 1 info.id from info where userid = t1.userid order by refreshtime desc

    ) 

    and t1.userid in

    (

        select  distinct userid from PackageRecord where PackagePid=1 and getdate()<endtime

    )

 

你可能感兴趣的:(sql)