简体   繁体   English

在Oracle SQL中加入多个条件

[英]Joining with multiple conditions in Oracle SQL

I've got a table of data of two columns (User_id and ForeignUser_id). 我有一个两列(User_id和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 User_ID类别ForeignUser_ID类别

I used union and cte functions but need to get the data in the structure as above so it wasn't helpful. 我使用了union和cte函数,但需要获取上述结构中的数据,因此它没有帮助。

the first table UserModel: 第一个表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)

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

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