paul bennett

Joins across different database tables

Posted on: March 18, 2008

Join tutorials always tell you how to join tables in the same database, but what if you need to join tables which sit in different databases?

Turns out this is nice and simple[1] (in MySQL, your db engine may vary):

SELECT this-db.table-one.column-one, other-db.table-two.column-two
FROM this-db.table-one
LEFT JOIN other-db.table-two
ON this-db.table-one.id = other-db.table-two.id

Simply put, the ‘dot operator’ lets you specify database as a parent of table in the same way you normally specify table as a parent to a column.

[1] assuming the account you use to connect to the database has rights across both databases. If not you’ll need to set up an account which does.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

%d bloggers like this: