Kartik Kartik - 3 months ago 8
SQL Question

Select limited rows from multiple tables

So this is fairly common knowledge to select rows from multiple tables and stack the results on top of each other:

SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
...


However, if I want only a limited number of rows from each table, then how should I write it?

SELECT * FROM table1 LIMIT 2
UNION
SELECT * FROM table2 LIMIT 2
UNION
...


Clearly doesn't work.

Note that in my case, I have 51 tables, all with the same exact columns.

Answer

could be work this way

( SELECT * FROM table1 LIMIT 2  )
UNION
( SELECT * FROM table2 LIMIT 2 )
UNION
...