【问题标题】:how to show data according to specific value如何根据特定值显示数据
【发布时间】:2015-05-02 11:23:12
【问题描述】:

我有一张这样的桌子:

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

如果我们考虑特定值 sonu 那么我想获取这样的数据...

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

请帮忙... 提前致谢.... 我已经从手机提交了这个问题,这就是为什么它可能不容易理解,但请为我尝试......

【问题讨论】:

    标签: mysql sql .net database


    【解决方案1】:

    奇怪的问题。可能最简单的方法是使用union all

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

    对于大表来说,这不是最有效的方法,但它可以正常工作——尤其是如果您在每个列上都有索引。

    【讨论】:

      【解决方案2】:

      您想要 f1 或 f2 包含“sonu”的所有行,但另一列的值?

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

      【讨论】:

      • 你的意思是or f2 = ...,而且这个case还有一个错字(应该是='sonu')。仍然让我投票。
      • @Zohar Peled:感谢您发现它,修复它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      相关资源
      最近更新 更多