Home >Database >Mysql Tutorial >How to Join Tables Across Different Databases in MySQL?
MySQL cross database table connection
MySQL supports connecting tables in different databases to achieve cross-database data retrieval.
Grammar:
To connect tables in different databases, use the following syntax:
<code class="language-sql">SELECT <... a.table1="" b.table2="" from="" join="" on="" t1="" t2="" t2.column2="t1.column1;
说明:
示例:
假设数据库A和B中存在以下表:
-- 数据库A CREATE TABLE user_profiles ( id INT PRIMARY KEY, name VARCHAR(50) ); -- 数据库B CREATE TABLE orders ( order_id INT PRIMARY KEY, user_id INT, product VARCHAR(50) );要将用户名映射到订单,可以执行如下连接:
SELECT up.name, o.product FROM A.user_profiles up JOIN B.orders o ON up.id = o.user_id;权限要求:
要执行跨数据库连接,用户帐户必须拥有访问这两个数据库及其相应表的必要权限。
The above is the detailed content of How to Join Tables Across Different Databases in MySQL?. For more information, please follow other related articles on the PHP Chinese website!