我们可以对两个不同的数据库表使用join吗?

时间:2022-06-24 14:33:23

Can we use the join operation for two tables from different databases? If yes, then how to do ?

我们可以对来自不同数据库的两个表使用join操作吗?如果是,怎么做?

Both databases are on the same server and DBMS is the same.

这两个数据库都位于同一个服务器上,而DBMS是相同的。

2 个解决方案

#1


118  

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully specify table names.

SQL Server允许您连接来自不同数据库的表,只要这些数据库位于同一服务器上。连接语法是相同的;惟一的区别是必须完全指定表名。

Let's suppose you have two databases on the same server - Db1 and Db2. Db1 has a table called Clients with a column ClientId and Db2 has a table called Messages with a column ClientId (let's leave asside why those tables are in different databases).

假设在同一个服务器上有两个数据库——Db1和Db2。Db1有一个名为client的表,其中包含一个列ClientId,而Db2有一个名为Messages的表,其中包含一个列ClientId(让我们来说明为什么这些表位于不同的数据库中)。

Now, to perform a join on the above-mentioned tables you will be using this query:

现在,要对上述表执行连接,您将使用以下查询:

select *
from Db1.dbo.Clients c
join Db2.dbo.Messages m on c.ClientId = m.ClientId

#2


0  

 SELECT ...
 FROM A.table t1
 JOIN B.table2 t2 ON t2.column = t1.col

#1


118  

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully specify table names.

SQL Server允许您连接来自不同数据库的表,只要这些数据库位于同一服务器上。连接语法是相同的;惟一的区别是必须完全指定表名。

Let's suppose you have two databases on the same server - Db1 and Db2. Db1 has a table called Clients with a column ClientId and Db2 has a table called Messages with a column ClientId (let's leave asside why those tables are in different databases).

假设在同一个服务器上有两个数据库——Db1和Db2。Db1有一个名为client的表,其中包含一个列ClientId,而Db2有一个名为Messages的表,其中包含一个列ClientId(让我们来说明为什么这些表位于不同的数据库中)。

Now, to perform a join on the above-mentioned tables you will be using this query:

现在,要对上述表执行连接,您将使用以下查询:

select *
from Db1.dbo.Clients c
join Db2.dbo.Messages m on c.ClientId = m.ClientId

#2


0  

 SELECT ...
 FROM A.table t1
 JOIN B.table2 t2 ON t2.column = t1.col