我们可以对两个不同的数据库表使用连接吗? [英] Can we use join for two different database tables?

查看:51
本文介绍了我们可以对两个不同的数据库表使用连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

两个数据库在同一台服务器上,DBMS 是一样的.

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

推荐答案

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

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.

假设您在同一台服务器上有两个数据库 - Db1Db2.Db1 有一个名为 Clients 的表,其中有一列 ClientIdDb2 有一个名为 Messages 带有 ClientId 列(让我们暂且不谈为什么这些表位于不同的数据库中).

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).

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

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

这篇关于我们可以对两个不同的数据库表使用连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆