Selecting two databases in one request

Good afternoon. I wanted to know if it is possible to select information from different databases in one request?


Answer 1, authority 100%

If a table is specified in the query with the database name database1.table1, then the table is selected from database1, if just table1, then from active database.

The general principle of cross-querying two databases within the same MySQL server:

SELECT t1.*, t2.*
FROM database1.table1 AS t1
INNER JOIN database2.table2 AS t2 ON t2.field1 = t2.field1

The aliases t1and t2are optional. But they reduce the query size and improve readability.


Answer 2, authority 20%

If the user under whose name you are working with the database server. have access to both databases, then the method described above will work.

SELECT `first_db`.`some_table`.`col_name`, `second_db`.`some_other_table`.`col_name`
FROM `first_db`.`some_table`
INNER JOIN `second_db`.`some_other_table` ON (`first_db`.`some_table`.`some_id` = `second_db`.`some_other_table`.`id`);

Answer 3

Yes, the answers are given, but the bases must be on the same server


Answer 4

You can!

SELECT `__`.``, `__`.`` FROM `__`, `__`