Jump to content

Query multiple databases in the same query


cyberRobot
Go to solution Solved by Barand,

Recommended Posts

When querying tables that are stored in different databases, I normally establish separate database connections, run separate queries, and combine the results in PHP. However, it appears that you can connect to one database and run a joined query on both databases, as long as the login credentials work for both databases.

 

With that said, does anyone know of an issue with writing a query like the one below?

SELECT 
    alias1.columnName1, 
    alias2.columnName2 
FROM       database1.table1  AS alias1 
LEFT JOIN  database2.table2  AS alias2  ON ...
 
Note that I wouldn't use names like database1, alias1, etc. And you can ignore the "..." in the ON clause.  :happy-04:
Link to comment
Share on other sites

  • Solution

However, it appears that you can connect to one database and run a joined query on both databases, as long as the login credentials work for both databases.

Yes, that's OK. So long as they are both on the same server. The connection is to the server. And, as you said, you have privileges to access all databases in the query.

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.