简体   繁体   English

T-SQL:从一个表中选择外键,然后在另一个表中找到对应的行

[英]T-SQL: Select foreign key from one table and find corresponding rows in another

I have two tables: tblA contains a foreign key which I want to use to pull corresponding rows from tblB. 我有两个表:tblA包含一个外键,我想用它来从tblB中提取相应的行。 The following queries will not work, but they explain what I want to do: 以下查询将不起作用,但它们说明了我想做什么:

SELECT [MyID]
FROM [tblA]

SELECT [MyColumn]
FROM [tblB]
WHERE [ID] = [tblA].[MyID]

This should be a fairly simple query but I am a noob with T-SQL. 这应该是一个相当简单的查询,但是我对T-SQL不满意。

Use a JOIN : 使用JOIN

SELECT A.MyID, B.MyColumn
FROM tblA A INNER JOIN tblB B
    ON A.MyID= B.ID

There are different types of joins , the INNER JOIN returns only rows from tblA with a matching key in tblB . 联接的类型不同, INNER JOIN仅从tblA返回行,并且在tblB具有匹配键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 T-SQL从一个表中选择列,其中另一个表中的id - T-SQL Select columns from one table where id in another table T-SQL Select 并从不同的表中计数? - T-SQL Select and count from different table? 在T-SQL中选择外键选择存储过程? - Handling foreign keys in T-SQL select stored procedure? 从模型中选择所有行,并将嵌套的json元素作为另一个模型的外键,并选择所有行 - Select all rows from a model and have a nested json element as the Foreign key to another model with all rows selected 在T-SQL中将多个行字段串联为一列 - Concatenating multiple rows fields into one column in T-SQL 如何从一个表中选择实际为文本的字段中的文本,并将相应的数字插入另一个表中? - How do I select text from one field that is actually text from one table, and insert the corresponding number into another table? 将一个表中的单个外键字段的多个值插入到另一个表中 - Insert multiple values for single foreign key field from one table into another table 将表上的ID主键设置为另一个表作为外键 - set id primary key from on table to another table as foreign key 将数据表行从一个表复制到另一个表 - copy datatable rows from one table to another 根据另一个表中的数据从表中选择行 - select rows from table based on data from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM