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