我试着跨多列(在多个表中)找到最大日期。我有一个SQL查询,我觉得几乎可以用,但是调试起来很难。问题是它从来没有回到任何东西。我做错了什么?我没有错,只是空行maxdate”。 6 Z5 }! ^4 h6 w' W- |/ w这是我的查询: # S7 K3 x- W9 \- aSELECT( SELECT MAX(dates) FROM ( SELECT dates = t1.UpdateDate UNION ALL SELECT dates = t2.UpdateDate UNION ALL SELECT dates = t3.UpdateDate UNION ALL SELECT dates = t4.UpdateDate ) as dateAcrossColumns) as maxdateFROM table1 as t1join table2 as t2 on t1.ID = t2.IDjoin table3 as t3 on t1.ID = t3.IDjoin table4 as t4 on t1.ID = t4.IDjoin table5 as t5 on t1.Status = t5.StatusWHERE t1.id = @param and t5.status 3 ! J+ z% k+ U3 a, @6 z 解决方案: ; u7 ]+ [. k+ r- f5 ?7 p0 x2 j- H 首先,您使用表1ID和表3的ID加入表2。我想你说表1。ID和表2的ID?# o7 S h6 O6 h+ E" {( O! e
join table2 as t2 on t1.ID = t3.ID应该: % h" I& J( ?, q; j3 Wjoin table2 as t2 on t1.ID = t2.ID现在变了; M9 D1 i- Z+ y% U0 ^3 x! k
我试过你的查询,它正常工作,你确定连接是否带回任何线路吗?要测试它,请替换它SELECT语句SELECT*,如果您不返回任何行,它们将被过滤掉joins /中间的位置WHERE。