简体   繁体   English

在两个不同数据库中的表之间加入?

[英]Join between tables in two different databases?

In MySQL, I have two different databases -- let's call them A and B .在 MySQL 中,我有两个不同的数据库——我们称它们为AB

Is it possible to perform a join between a table that is in database A , to a table that is in database B ?是否可以在数据库A中的表与数据库B 中的表之间执行连接?

Yes, assuming the account has appropriate permissions you can use:是的,假设该帐户具有适当的权限,您可以使用:

SELECT <...>
FROM A.table1 t1 JOIN B.table2 t2 ON t2.column2 = t1.column1;

You just need to prefix the table reference with the name of the database it resides in.您只需要在表引用前面加上它所在的数据库的名称。

SELECT <...> 
FROM A.tableA JOIN B.tableB 
SELECT *
FROM A.tableA JOIN B.tableB 

or或者

SELECT *
  FROM A.tableA JOIN B.tableB
  ON A.tableA.id = B.tableB.a_id;
SELECT <...>
FROM A.table1 t1 JOIN B.table2 t2 ON t2.column2 = t1.column1;

Just make sure that in the SELECT line you specify which table columns you are using, either by full reference, or by alias.只需确保在 SELECT 行中通过完整引用或别名指定您正在使用的表列。 Any of the following will work:以下任何一项都将起作用:

SELECT *
SELECT t1.*,t2.column2
SELECT A.table1.column1, t2.*
etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM