找到你要的答案

Q:how to show data according to specific value

Q:如何根据具体值显示数据

I have a table like this:

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

if we consider the specific value sonu then I want to fetch data like this...

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

please help... thanks in advance.... I have submitted this question from mobile that's why it may be not easy to understand but please try for me...

我有像这样的桌子:

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

如果我们考虑具体的价值该然后我想拿这样的数据…

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

please help... thanks in advance.... I have submitted this question from mobile that's why it may be not easy to understand but please try for me...

answer1: 回答1:

Curious problem. Probably the easiest way is with 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 of the columns.

好奇的问题。也许最简单的方法是结合所有:

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

对于大表来说,这不是最有效的方法,但是它会工作的很好--尤其是如果你对每个列有索引的话。

answer2: 回答2:

You want all rows where f1 or f2 contain 'sonu', but the value of the other column?

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

你想要所有的排在F1或F2包含“该”,但价值的其他列?

select 
   case when f1 = 'sonu' then f2 else f1 end
from tab
where f1 = 'sonu' or f2 = 'sonu'
mysql  sql  .net  database