join two tables in different databases

Wednesday 7th November 2007 10:35 PM

TAGS: mySQL, join

Interesting little tip that I must admit never knew was possible (although admitadly, probably isn't needed to be used *that* often).

Suppose you have a shared database of users, and perhaps a small sub-system that has it's own self-contained database, but needs to run queries that include the users in the shared database. Provided the mySQL user has permissions to access both databases, you can simple run a query like:

SELECT database1.sharedUsers.*, database2.userTable.*
FROM database1.sharedUsers
LEFT JOIN database2.userTable.userID ON database1.sharedUsers.userID

Comments on this article:


#1 Bill says:

Friday 4th April 2008 04:28 AM

Looks like you are missing a an element in this query. Change last line to:

LEFT JOIN database2.userTable
ON database2.userTable.userID = database1.sharedUsers.userID


Add Comment:


Make a Comment

*Nb, all comments are moderated to prevent spam or inappropriate content.








netforge logo
netforge provides high quality and friendly website design services to business. We're Australian based and reliable... (find out more).