SQL 如果两个表在不同的模式中,如何连接它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5813621/
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-09-01 10:19:57  来源:igfitidea点击:

How to join two tables if they are in different schemas

sqlsql-server

提问by vivek kumar luetel

I have two different schemas in SQL Server (say S1, S2). And two tables in those schemas(say S1.Table1, S2.Table2). I want to query these two tables from schema S1.

我在 SQL Server 中有两种不同的架构(比如 S1、S2)。以及这些模式中的两个表(比如 S1.Table1、S2.Table2)。我想从模式 S1 查询这两个表。

Both S1 and S2 are in SQL Server 2005 databases. I want to do something like this:

S1 和 S2 都在 SQL Server 2005 数据库中。我想做这样的事情:

select T1.Id
  from S1.Table1 T1
     , S2.Table2 T2 
 Where T1.Id = T2.refId

回答by gbn

Use 3 part object names to specify the database: I assume you mean "database" not "schema" (in say the Oracle sense)

使用 3 部分对象名称来指定数据库:我假设您的意思是“数据库”而不是“架构”(在 Oracle 意义上)

select T1.Id
from 
  DB1.schema.Table1 T1
 JOIN
   DB2.schema.Table2 T2 ON T1.Id = T2.refId

Note the better way of doing JOINs...

请注意执行 JOIN 的更好方法...

回答by M.R.

You didn't mention if the DBs were on the same server. If yes, you can follow the answer above. If not, you will need to create a linked server one of the servers, and then you can reference the linked server via

你没有提到数据库是否在同一台服务器上。如果是,您可以按照上面的答案进行操作。如果没有,您需要在其中一台服务器上创建一个链接服务器,然后您可以通过以下方式引用链接服务器


select T1.Id
  from [linkedservername].DB1.Schema.Table1 T1
     , S2.Table2 T2 
 Where T1.Id = T2.refId

回答by Anthony

Select T1.Id

FROM

s1.Table1 T1

JOIN

s2.Table2 T2

WHERE

T1.Id = T2.refId;

This is the way to do your query on MySQL. I would assume it also works in Oracle 11g.

这是在 MySQL 上进行查询的方法。我认为它也适用于 Oracle 11g。