Home > Net >  How to get the MAX of the same column across several tables?
How to get the MAX of the same column across several tables?

Time:12-10

I've got several tables which are mostly different but all with a column called updated_at which contains values like "2021-11-15 13:47:36" (column type is timestamp). I need to get the single max updated_at value across all the tables. Assume I use MAX() on a JOIN of all tables somehow? Or is there a better way?

Thanks

CodePudding user response:

Use UNION:

SELECT MAX(updated_at)
FROM (
    SELECT MAX(updated_at) AS updated_at FROM table1
    UNION ALL
    SELECT MAX(updated_at) AS updated_at FROM table2
    UNION ALL
    SELECT MAX(updated_at) AS updated_at FROM table3
) AS combined
  • Related