简体   繁体   中英

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

Use a 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM