I have multiple databases in MySQL from Diffrent Companies. Ex.
1.Company1
2.Company2
3.Company3
4.Company4
In every database the table and column's structure are same but data is stored for different companies. Now if i have to get a count of EmployeeID's sales for diffrent companies then i need to write the queries like below.
Select a.EmployeeID,Count(b.TransactionDate)
From Company1.Employee as a
Inner Join Company1.Sales as b
On a.EmployeeID=b.EmployeeID
Group By a.EmployeeID
Union
Select a.EmployeeID,Count(b.TransactionDate)
From Company2.Employee as a
Inner Join Company2.Sales as b
On a.EmployeeID=b.EmployeeID
Group By a.EmployeeID
Union
Select a.EmployeeID,Count(b.TransactionDate)
From Company3.Employee as a
Inner Join Company3.Sales as b
On a.EmployeeID=b.EmployeeID
Group By a.EmployeeID
Union
Select a.EmployeeID,Count(b.TransactionDate)
From Company4.Employee as a
Inner Join Company4.Sales as b
On a.EmployeeID=b.EmployeeID
Group By a.EmployeeID
Notice i am changing the database in "FROM" Clause and "INNER JOIN" with hard-coded value.
In future further database may be added and i don't want to change the code behind or i don't want add code with another "union". Is there anything which we can do to do it dynamically. i mean if we can store database name in a table and query should automatically pick those database information from the table.