Select two or more tables from different databases

how to use two tables in one query that are in different databases means

SELECT table1.id, table1.name, table2.id, table2.telephone
FROM table1, table2   
WHERE table1.id = table2.id

here table1and table2are in a separate database.

+5
source share
2 answers

You can create cross-databases, no problem. Just a prefix to the name of your table with the database name.

SELECT t1.id, t1.name, t2.id, t2.telephone
FROM db1.table1 t1
INNER JOIN db2.table2 t2 on t1.id = t2.id;

Be careful with permissions. If the user does not have access to one of the databases, this selection will not be made.

+10
source

You need to use fully qualified table names as well as fields / attributes:

SELECT table1.id, table1.name, table2.id, table2.telephone
FROM db_1.table1, db_2.table2
WHERE table1.id = table2.id
+1
source

All Articles