Jump to content

Archived

This topic is now archived and is closed to further replies.

diskhub

Joining tables from two different databases

Recommended Posts

first you need to have access to both database with the same username and password. assume we have table1 in db1 and table2 in db2

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] t1.* FROM db1.table1 AS t1, db2.table2 AS t2 WHERE t1.id=t2.id [!--sql2--][/div][!--sql3--]

 

As simple as that. You can also use JOIN syntax.

Share this post


Link to post
Share on other sites

The inconvenience of having to have similar login credentials to both DBs precludes to use of this technique for any DB that you haven't explicitly set up in this fashion. If it's up to you, keep the related tables in the same DB; if it's not, consider doing this in middleware or via temporary tables, otherwise you'll get caught with your pants down in the credentials ever change!

Share this post


Link to post
Share on other sites

[!--quoteo(post=330045:date=Dec 23 2005, 04:54 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 23 2005, 04:54 PM) 330045[/snapback][/div][div class=\'quotemain\'][!--quotec--]

The inconvenience of having to have similar login credentials to both DBs precludes to use of this technique for any DB that you haven't explicitly set up in this fashion. If it's up to you, keep the related tables in the same DB; if it's not, consider doing this in middleware or via temporary tables, otherwise you'll get caught with your pants down in the credentials ever change!

 

I suppose the other important issue is making sure there is a common relational key (ID) between the 2 DB tables.

Share this post


Link to post
Share on other sites

×

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.