How to show data according to a specific value

I have a table like this:

----------------------------------
f1.          :           f2
----------------------------------
sonu         :        monu
rakesh       :        sonu
vivek        :        monu
raju.        :        sonu
sonu         :        umesh
ramesh       :        sonu
-----------------------------------------

      

if we look at a specific sonu value then I want to get data like this ...

----------------
 c1
------------------
monu
rakesh
raju
umesh
ramesh
---------------------

      

Please help ... thanks in advance .... I asked this question from mobile, so it might not be easy to understand, but please try me ...

+3


source to share


2 answers


An interesting problem. Probably the easiest way is union all

:

select f2
from table t
where f1 = 'sonu'
union all
select f1
from table t
where f2 = 'sonu';

      



This is not the most efficient method for a large table, but it will work fine - especially if you have indexes on each column.

+4


source


Do you want all rows where f1 or f2 contains "sonu", but the value of another column?



select 
   case when f1 = 'sonu' then f2 else f1 end
from tab
where f1 = 'sonu' or f2 = 'sonu'

      

+3


source







All Articles