我有两张表table 1
和table 2
,
table 1
的列名为sno
和useddate
table 2
的列名为sno
和recentlyuseddate
我想比较这两个列useddate
和recentlyusedate
并获取最近使用的日期。
这些表可能有很多条目,但在比较这些表后我只需要一个ENTRY(最近的)日期。
答案 0 :(得分:1)
SELECT MAX(useddate) AS mostrecent
FROM
(SELECT useddate FROM Table1
UNION ALL
SELECT recentlyuseddate AS useddate FROM Table2) TheUnion
答案 1 :(得分:0)
您可以使用联盟
SELECT MAX(col)
FROM (SELECT col FROM TABLE_1
UNION ALL
SELECT col FROM TABLE_2)
答案 2 :(得分: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;
此版本还会告诉您日期来自哪个表格。