-2

I have a schema in MySQL with many tables that all use the same column name for their primary key, id, which is a bigint in all tables. What I need is a query that will find the maximum value of id across all rows across all tables. Alternatively I would settle for a list of the max id values of each table, which I could then sort.

I have this which will list all tables that have the id column, but leaping from there to selecting the columns across those tables is beyond my SQL skills:

SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME = 'id'
E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • 1
    You will need to dynamically build and execute the query – Stu Aug 15 '23 at 20:03
  • I figured there would be some kind of dynamicity involved, but like I said that's far beyond my SQL skills. I don't have a DBA or other developer who knows SQL to that level available, so an answer with the details is welcomed. – E-Riz Aug 15 '23 at 20:29
  • I don't get the duplicate votes - this is a very different question than the other one. Even if the solutions are similar, the questions are not at all. – E-Riz Aug 15 '23 at 20:34
  • 1
    Agreed: the dupe target asks about [Teradata](https://en.wikipedia.org/wiki/Teradata), not MySQL. Voting to reopen (though there might be another, better dupe target out there...) – ChrisGPT was on strike Aug 16 '23 at 14:21

0 Answers0