0

我有两张桌子说table 1table 2

table 1列名称为snouseddate table 2列名称为snorecentlyuseddate

我想比较这两列useddaterecentlyusedate获取最近使用的日期。

这些表可能有很多条目,但在比较这些表之后,我只需要一个条目(最近的)日期。

4

3 回答 3

1
SELECT MAX(useddate) AS mostrecent
FROM 
(SELECT useddate FROM Table1
UNION ALL
SELECT recentlyuseddate AS useddate FROM Table2) TheUnion
于 2013-06-06T13:47:16.763 回答
0

如果你有两个表中两个日期的索引,我会选择:

select top 1 sno, useddate, which
from ((select top 1 sno, useddate, 'table1' as which from table1 order by useddate desc) union all
      (select top 1 sno, recentlyuseddate , 'table2' as which from table2 order by recentlyuseddate desc)
     )
order by useddate desc;

此版本还告诉您日期来自哪个表。

于 2013-06-06T13:49:13.383 回答
0

您可以为此使用工会

 SELECT MAX(col)
 FROM (SELECT col FROM TABLE_1
    UNION ALL
    SELECT col FROM TABLE_2)
于 2013-06-06T13:34:27.560 回答