下面是SQL Server 2008中我的两个表的架构
Table 1
StartDate,EndDate,ID,country
Table 2
StartDate,EndDate,ID,country
我想更新下面的表1
UPDATE t1
SET t1.startdate=min(t2.startdate),
t1.enddate=max(t2.enddate)
FROM table1 t1,
table 2 t2
WHERE t1.country=t2.country
GROUP BY t2.country
目前我正在使用子查询来更新startdate和enddate,但我不喜欢这样。
请建议一些更好的解决方案
答案 0 :(得分:3)
我认为SQL 2008是Sql Server 2008.因为UPDATE ... FROM不是标准的ANSI,而只适用于Sql Server
<强>更新强>
试试这个:
update table1
set table1.startdate=t.l,
table1.enddate=t.u
from
(select MAX(t2.startdate) as l, MAX(t2.enddate) as u, t2.idcountry as idc
from table2 t2
where t2.idcountry = table1.idcountry
group by t2.idcountry) as t
where table1.idcountry = t.idc