简体   繁体   中英

How to join two tables in this example

I got a problem getting records out of two tables correctly.

I need to get a list in which every device and its software is linked.

Hard to explain what my goal is, but i hope you will understand my Example.

Example:

在此输入图像描述

You need a join with multiple instances of table2

select b.name,c.name
from table1 a inner join table2 b on a.targetid=b.objectid
inner join table2 c on a.assignedid=c.objectid

JOIN the Table2 two times with Table1 and add the proper alias names will solve in your request.

The working query is:

SELECT T2.Name AS ComputerName, T3.Name AS SoftwareName
FROM Table1 T1
JOIN Table2 T2 T2.ObjectId = T1.TargetId
JOIN Table2 T3 T3.ObjectId = T1.AssignedId

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