SQL Server Double INNER JOIN in SQL

af7jpaap  于 2023-11-16  发布在  其他
关注(0)|答案(2)|浏览(93)

I have a table i'm trying to INNER JOIN twice. Here's my example:

Table1:
id1, id2, otherInfo

Table2:
id, Name, phone, address

In this example, I need to get the name and phone for id1 and id2. How do I accomplish this? How do I handle if id1 = 0?

thigvfpy

thigvfpy1#

You can try with inner join :

select t2.name
     , t2.phone
     , t3.name
     , t3.phone
from table1 t1
join table2 t2 on t1.id1 = t2.id
join table2 t3 on t1.id2 = t3.id

In case id1 or id2 can be null , use left join instead.

wlzqhblo

wlzqhblo2#

Your question is a bit vague but I think this is what you are looking for.

select t1.otherInfo
    , t2.Name
    , t2.Phone
from Table1 t1
join Table2 t2 on t2.id = t1.id1 OR t2.id = t1.id2

相关问题