开发者

Get MAX() on column in two tables

I 开发者_高级运维have two tables which both have the column DateTime.

How can I get the MAX() DateTime?

The shorter/simpler the better, because this is just part of a larger query.


You could use the GREATEST function:

SELECT GREATEST((SELECT MAX(column) 
                   FROM TABLE_1),
                (SELECT MAX(column) 
                   FROM TABLE_2))

Using UNIONs:

SELECT MAX(col)
  FROM (SELECT col FROM TABLE_1
        UNION ALL
        SELECT col FROM TABLE_2)

Use UNION ALL for this - it's faster because it doesn't remove duplicates, and it doesn't matter if duplicates are returned by the subquery in this example.


SELECT MAX(thedate) FROM (
    SELECT mydate as thedate FROM TABLE1

    UNION

    SELECT anotherdate as thedate FROM TABLE2
) as tablealias
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜