Generally, you can combine results from multiple selects or tables using UNION
or even UNION ALL
on the database level. You just have to take care that your tables/selects return the same columns.
For example:
-- old table/database
select t.customername as name, t.street as street, t.city as city
from old_data.customers t
where t.name like 'M%'
union
- new table/database
select p.name as name, a.street as street, a.city as city
from new_data.person p
inner join new_data.address a on p.address_id = a.id
where p.name like 'M%'
With UNION
it does not matter which names the tables or columns in the single SELECT
statements have. What counts is that there are the same amount of columns and that they have compatible types.
The actual problem may lie in accessing the tables from multiple databases. The solution for that depends on the DBMS you are using and where your old and new databases are located.
If both databases are stored on the same server, as in your case, most DMBS (such as MySQL) will work for the query above (you prepend the database name to the table name as in database.table
and you are good).
If both databases were not stored on the same server, then the solution would depend on the DBMS. Oracle, for example, lets you specify database links, which you can use to span queries across multiple servers. Or, you can cache remote tables using materialized views.
With MySQL, there are different possibilities, which are discussed in this question, e.g.