sql Left Join 右表取一条数据,三种方式

一、

left join

(select l.* from 
  (select RealName,CompanyId,row_number() over (partition by CompanyId order by id desc) as rownum from vm_User WHERE IsAdmin=1) l
   where l.rownum = 1 )  w

二、left join (select MAX(RealName),CompanyId, from vm_User GROUP BY CompanyId) W

三、

用OUTER APPLY
select b.* FROM a表 a
OUTER APPLY
(select TOP(1) * from b表 WHERE [Name] = a.[AName] ORDER BY BNo desc) b

你可能感兴趣的:(sql,sql)