如何使用 SQL 跨两个表选择数据,其中每个表中的字段匹配?

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

How do i select data across two tables using SQL where a field from each table matches?

sqloracle

提问by Shane

Delphi 2010 & Oracle Database

Delphi 2010 和 Oracle 数据库

I need to write a select statement across two tables

我需要跨两个表编写一个选择语句

Accounts & Master

帐户和主

From the Accounts table, I need to select the Account_Id, Account_Number, Bank_Id, and External_Code

从 Accounts 表中,我需要选择 Account_Id, Account_Number, Bank_Id, and External_Code

From the Master table, i need to select the Account_String.

从主表中,我需要选择Account_String.

The Master's Account_String field matches the Account's Extenal_Code field

Master 的 Account_String 字段匹配 Account 的 Extenal_Code 字段

thanx

谢谢

回答by Eric Petroelje

Sounds like a simple join, unless I'm missing something:

听起来像一个简单的连接,除非我遗漏了什么:

SELECT a.Account_Id, a.Account_Number, a.Bank_Id, a.External_Code, m.Account_String 
FROM Accounts a
INNER JOIN Master m ON m.Account_String = a.External_Code

回答by Scott Hunter

Standard SQL:

标准 SQL:

select Accounts.Account_id, Accounts.Account_Number, Accounts.Bank_Id,
    Accounts.External_Code, Master.Account_String  
from Accounts, Master
where Accounts.External_Code = Master.Account_String;

Note: You probably don't need both Accounts.External_Code and Master.Account_String in the result, as the query guarantees they are the same.

注意:结果中可能不需要 Accounts.External_Code 和 Master.Account_String,因为查询保证它们是相同的。

回答by gpojd

You want an inner join.

你想要一个内部连接

SELECT a.account_id, a.account_number, a.bank_Id, a.external_code, m.account_string
FROM accounts a JOIN master m ON a.external_code = m.account_string
WHERE ...;