【问题标题】:Remove specific fields and show other fields in Mysql删除特定字段并显示Mysql中的其他字段
【发布时间】:2020-07-19 22:59:56
【问题描述】:

我想询问解决这个 MySQL 问题的方法。 目前,我有这个选择查询

SELECT username, friend_username from friends
WHERE (username = "apple" or friend_username = "apple");

它显示了这个朋友表(如下)。

Friends Table
------------------------------
| username | friend_username |
------------------------------
| apple    | orange          |
| apple    | pear            |
| durian   | apple           |
------------------------------

但我想执行一个选择查询,使其看起来像这样(如下)。

------------
| username |  
------------
| orange   |
| pear     |
| durian   |
------------

有没有可能的方法来实现这一点?感谢帮助!谢谢。

【问题讨论】:

    标签: mysql sql select where-clause


    【解决方案1】:

    我会这样写:

    select case when username = 'apple' then friend_username else username end username
    from friends
    where 'apple' in (username, friend_username);
    

    where 子句使用in 过滤usernamefriend_username 等于'apple' 的行。然后,select 子句中的case 表达式显示“其他”列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多