[英]Display SQL Table records that have matching data from different table
I am very new to SQL, and I am trying to filter a set of records from one table based on matching columns from another table.我对 SQL 很陌生,我试图根据另一个表中的匹配列过滤一个表中的一组记录。 Here is an example of what I would like to do: I have two tables, I'll call them TableA and TableB.这是我想要做的一个例子:我有两个表,我将它们称为 TableA 和 TableB。 I have already applied a search on both Tables to filter their separate results down, but I want to filter TableA based on matching records from TableB.我已经在两个表上应用了搜索来过滤它们单独的结果,但我想根据来自 TableB 的匹配记录过滤 TableA。 TableA:表A:
Desired Result in TableA format: TableA 格式的期望结果:
TableB and TableA have matching ID columns, and I only want to display the records in TableA if the records also appear in TableB, but I need them to appear in TableA format. TableB 和 TableA 有匹配的 ID 列,如果记录也出现在 TableB 中,我只想显示 TableA 中的记录,但我需要它们以 TableA 格式出现。
一个简单的连接就可以做到这一点。
select TableA.* from TableA inner join TableB on TableB.ID = TableA.IB
You can use exists
:您可以使用exists
:
select a.*
from a
where exists (select 1 from b where a.id = b.id);
你需要做类似的事情
Select a.id, a.first_name, a.last_name, a.birthday, a.ssn from TableA a, Table b where a.id=b.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.