SELECT B105L.文章ID, B105L.发表时间, B105L.作者,
IIF(ISNULL((SELECT TOP 1 更新时间 FROM B105M WHERE 文章ID=B105L.文章ID ORDER BY 更新时间 desc )),发表时间,
(SELECT TOP 1 更新时间 FROM B105M WHERE 文章ID=B105L.文章ID ORDER BY 更新时间 desc))
FROM B105L;
select a.文章id, a.作者, iif(nz(c.更新时间之max,"")="" , a.发表时间,c.更新时间之max) as 最后更新时间
from 表1 as a
left join (select b.文章id, max(b.更新时间) as 更新时间之max from 表2 as b group by b.文章id) as c on a.文章id = c.文章id;
select a.文章id, a.作者, iif(nz(c.更新时间之max,"")="" , a.发表时间,c.更新时间之max) as 最后更新时间
from 表1 as a
left join (select b.文章id, max(b.更新时间) as 更新时间之max from 表2 as b group by b.文章id) as c on a.文章id = c.文章id;
select t.id,t.name,(case when u.dt is null then t.dt else u.dt end)as updatedt from testta t left join (select id,max(dt) as dt from updatedate group by id) as u on t.id=u.id
Create Table 表2
(文章ID Int,
更新时间 Varchar(10))
--插入數據
Insert 表1 Select 1, 'fssds', '1985-5-5'
Union All Select 2, 'ffsdfs', '1990-5-12'
Insert 表2 Select 1, '1988-8-8'
Union All Select 1, '1990-8-5'
--測試
Select
A.文章ID,
A.作者,
IsNull(B.更新时间,A.发表时间) As 最后更新时间
from 表1 A
Left Join (Select 文章ID,Max(更新时间) As 更新时间 from 表2 Group By 文章ID) B
On A.文章ID=B.文章ID
--刪除測試環境
Drop Table 表1,表2
--結果
/*
文章ID 作者 最后更新时间
1 fssds 1990-8-5
2 ffsdfs 1990-5-12
*/
這個都行
select 表1.文章ID,表1.作者,(case when A.更新時間 is null then 發表時間 else A.更新時間 end) as 最後更新時間 from 表1
left join (select 文章ID,max(更新時間) as 更新時間 from 表2 group by 文章ID) A on 表1.文章ID=A.文章ID
select 文章ID,作者,(case when 更新時間 is null then 發表時間 else 更新時間 end) as 最後更新時間 from (
select 表1.文章ID,表1.作者,表1.發表時間,max(表2.更新時間) as 更新時間 from 表1
left join 表2 on 表1.文章ID=表2.文章ID
group by 表1.文章ID,表1.作者,表1.發表時間
)A