sql server 左链接为准 ,匹配不到,空填充

SELECT *,PR.OrderNo FROM TB_USER u
OUTER APPLY (
    SELECT TOP
        1 R.id,
        R.OrderNo
    FROM
        tb_order ( NOLOCK ) R 
    WHERE
       R.ID = u.ORDERID ORDER BY OutTime desc
    ) AS PR 
 WHERE u.IsDel = 0

 

 

SELECT u.LoginName,pr.OutTime,pr.Id,pr.OrderNo FROM TB_USER u
OUTER APPLY (
    SELECT TOP
        1 R.id,
        R.OrderNo,
        R.OutTime
    FROM
        tb_order ( NOLOCK ) R 
    WHERE
        R.OutTime is not null and r.IsDel = 0 ORDER BY OutTime desc
    ) AS PR 
 WHERE u.IsDel = 0
        

posted @ 2024-03-14 11:28  cclon  阅读(21)  评论(0编辑  收藏  举报