Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

sql - Get column names if value is true

Say I have table:

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

How can I display the column name "cow" ?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Only simple way to do that - checking directly each column:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...