Home > Database > Mysql Tutorial > How to Join Tables Across Different Databases in MySQL?

How to Join Tables Across Different Databases in MySQL?

DDD
Release: 2025-01-18 03:07:09
Original
729 people have browsed it

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代表数据库名称。
  • table1和table2代表要连接的表名。
  • t1和t2是为表分配的别名。
  • ON子句指定连接条件,该条件将来自两个表的列关联起来。

示例:

假设数据库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;

权限要求:

要执行跨数据库连接,用户帐户必须拥有访问这两个数据库及其相应表的必要权限。

Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template