【问题标题】:Get column names if value is true如果值为 true,则获取列名
【发布时间】:2018-09-28 13:49:23
【问题描述】:

假设我有桌子:

table_name: animal
column_names:
"dog": 'f',
"cat": 'f',
"cow": 't'
.
.
.

如何显示列名“cow”?

谢谢!

【问题讨论】:

  • 这个问题有点不清楚,至少对我来说是这样。您能否分享一些示例数据以及您尝试获得的结果?
  • 这是什么意思?您有一个名为 animal 的表,其 column_names (狗、猫、牛)?这些列的值是 f,f,t?你有什么问题?到目前为止,您尝试过什么?
  • 你知道SELECTWHERE是什么吗?
  • 这听起来像是一个糟糕的设计,您不应该为每个动物设置一列,而应有一列包含动物的类型。毕竟动物不可能同时是牛和狗。

标签: sql postgresql


【解决方案1】:

这样做的唯一简单方法 - 直接检查每一列:

select 
  case 
    when dog=true then 'dog'
    when cat=true then 'cat'
    when cow=true then 'cow'
  end as col
from animal

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2021-02-21
    • 2017-12-07
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多