Selecting the same value from two columns in the lookup table

I have a table as follows

UserID CONSUMERID 1 2 1 3 1 4 5 1 

this is a search column, the user can be here. I need to query the table and get one column output, so that, for example, if ID = 1, I could query userid and get all the data of the consumed resource, where userid = 1, and at the same time, the request to get all the user data, where customerid = 1 .. all results should be in one column. In other words, query both columns for 1 and return the opposite column value.

+4
source share
2 answers

Use UNION (implicit DISTINCT) or UNION ALL , for example:

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

SQL Fiddle Demo

+3
source

You can do this without combining, just using the logic:

 select (case when UserId = 1 then Consumerid else UserId end) as id from t where UserId = 1 or ConsumerId = 1 
0
source

All Articles