4

I have a table as follows

UserID  CONSUMERID
1          2
1          3
1          4
5          1

this is a lookup column, user can be consumer here.
I need to query the table and get a single column output so that for example if ID = 1 I should be able to query userid and get all consumnerid data where userid = 1 and at the same time query get all userid data where consumerid = 1.. all the results should come in one column. So in other words query both columns for 1 and return opposite column value.

4

2 回答 2

3

使用UNION(implicit DISTINCT) or UNION ALL,像这样:

SELECT UserId FROM table WHERE ConsumerId = 1
UNION ALL
SELECT ConsumerId FROM table WHERE UserID = 1;

SQL 小提琴演示

于 2012-11-30T12:45:10.353 回答
0

您可以在没有联合的情况下执行此操作,只需使用逻辑:

select (case when UserId = 1 then Consumerid else UserId end) as id
from t
where UserId = 1 or ConsumerId = 1
于 2012-11-30T14:21:22.980 回答