当前位置: 动力学知识库 > 问答 > 编程问答 >

Joining with multiple conditions in Oracle SQL

问题描述:

I've got a table of data of two columns (User_id and ForeignUser_id). So these columns I also have in the other table with a column of category (one for all). What should I do to get the set of data like

User_ID Category ForeignUser_ID Category

I used union and cte functions but need to get the data in the structure as above so it wasn't helpful.

the first table UserModel:

User_ID ForeignUser_ID

1234 2568

1234 6589

3333 4426

3333 2635

4252 6235

4252 5986

the second table Users:

 User_ID Category

1234 A

3333 B

4252 A

2568 B

6589 B

4426 A

2635 C

6235 C

5986 B

The desired outcome:

User_ID Category ForeignUser_ID Category

1234 A 2568 B

1234 A 6589 B

3333 B 4426 A

3333 B 2635 C

4252 A 6235 C

4252 A 5986 B

网友答案:

you will need to do two joins to get this result

Select um.User_ID, us1.Category, um.ForeignUser_ID, us2.Category    
From UserModel um inner join Users as us1 on (um.User_ID = us1.User_ID)
                  inner join Users as us2 on (um.ForeignUser_ID = us2.User_ID)
分享给朋友:
您可能感兴趣的文章:
随机阅读: