Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select two or multiple tables from different databases [closed]

Tags:

php

mysql

how to use two tables in a single query, which are in different databases, means,

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

here, table1 and table2 are in separate database.

like image 533
Bhavesh G Avatar asked Dec 12 '22 06:12

Bhavesh G


1 Answers

You can do cross-database joins, no problem. Simply prefix your table name with 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 wary of permissions, though. If a user doesn't have access to one of the databases, this select will fail.

like image 72
Sergio Tulentsev Avatar answered Jan 20 '23 00:01

Sergio Tulentsev