There are times when you want to perform joins between tables across multiple databases. To do this, qualify table and column names sufficiently so that MySQL knows what you’re referring to. For example, the following statement uses the two tables to associate artists with their paintings:

SELECT, painting.title
  FROM artist INNER JOIN painting
  ON artist.a_id = painting.a_id;

But suppose instead that artist is in the db1 database and painting is in the db2 database. To indicate this, qualify each table name with a prefix that specifies which database it’s in. The fully qualified form of the join looks like this:

SELECT, db2.painting.title
  FROM db1.artist INNER JOIN db2.painting
  ON db1.artist.a_id = db2.painting.a_id;

If there is no default database, or it is neither db1 nor db2, it’s necessary to use this fully qualified form. If the default database is either db1 or db2, you can dispense with the corresponding qualifiers. For example, if the default database is db1, you can write like this:

SELECT, db2.painting.title
  FROM artist INNER JOIN db2.painting
  ON artist.a_id = db2.painting.a_id;

Hope this helps.