How to perform cross-database queries in MySQL?

There are two methods for performing cross-database queries in MySQL.

  1. Utilize fully qualified table names: Add the database name as a prefix to the table name in the query statement, for example:
SELECT db1.table1.column1, db2.table2.column2 
FROM db1.table1 
JOIN db2.table2 ON db1.table1.id = db2.table2.id;
  1. You can switch to the database you want to query first using the USE statement, and then directly query the table. For example:
USE db1;
SELECT column1 FROM table1;
USE db2;
SELECT column2 FROM table2;

Cross-database queries can be achieved using any method. However, it is important to note that such queries may impact performance, so it is advised to minimize them when designing databases.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds