MYSQL Select tableX from all databases

642 Views Asked by At

Let's say I have 50 databases that all have same schema and all have table 'plays'.

Is there any possible way to SELECT all the data from table 'plays' from all the databases at once ?

Is there any way to do this if I dont know how many databases I have ?

Currently I would do this :

(SELECT * FROM db1.plays WHERE condition)
UNION
(SELECT * FROM db2.plays WHERE condition)
UNION...
(SELECT * FROM db50.plays WHERE condition) 

Is there any way to write less code for this ?

Regards

1

There are 1 best solutions below

0
On BEST ANSWER

I'm not sure, but to shorten your code, you could do

Select * FROM db1.plays, db2.plays, ...., db3.plays WHERE <condition>

Another thing that comes to mind is using the table INFORMATION_SCHEMA. Maybe someone else has a better idea.