MySQL 连接两个不同数据库中的表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5698378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:33:01  来源:igfitidea点击:

Join between tables in two different databases?

mysqlsqljoin

提问by user3262424

In MySQL, I have two different databases -- let's call them Aand 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 中的表之间执行连接?

回答by OMG Ponies

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.

您只需要在表引用前面加上它所在的数据库的名称。

回答by Senthil

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

回答by Kalaivani M

SELECT *
FROM A.tableA JOIN B.tableB 

or

或者

SELECT *
  FROM A.tableA JOIN B.tableB
  ON A.tableA.id = B.tableB.a_id;

回答by Noel Swanson

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. Any of the following will work:

只需确保在 SELECT 行中通过完整引用或别名指定您正在使用的表列。以下任何一项都将起作用:

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