Select columns across different databases

Mysql

Mysql Problem Overview


Is it possible to do a select ( or insert) statement across different databases that are located on the same server? If yes, how?

Mysql Solutions


Solution 1 - Mysql

You would specify the database by using the syntax databasename.tablename

Example:

SELECT 
    mydatabase1.tblUsers.UserID, 
    mydatabse2.tblUsers.UserID
FROM 
   mydatabase1.tblUsers
       INNER JOIN mydatabase2.tblUsers 
           ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID

Solution 2 - Mysql

You can select from any other table with a JOIN statement and using this type of syntax.

SELECT A.*, B.* FROM db1.table1 A LEFT JOIN db2.table1 B ON A.id = B.id

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGravitonView Question on Stackoverflow
Solution 1 - MysqlTheTXIView Answer on Stackoverflow
Solution 2 - MysqlÓlafur WaageView Answer on Stackoverflow