我想找到列值为1 AND userid = 3的列名
例如我有一张桌子



 quesitonid   userid      answer1 answer2 answer3
  1            1          0         1        0
  2            2          1         0        0
  3            3          0         0       1


结果将产生为

  question1   userid   answer3
     3           3         1


我该如何使用sql?我做了很多尝试,但没有成功,如果有人能帮助的话,他要先感谢一百万。

这是我的SQL查询

SELECT * from questions Q WHERE  Q.userid=1 AND (Q.answer1=1 OR Q.answer2=1 OR Q.answer3=1)

最佳答案

尝试这个

SELECT *
,CASE
    WHEN ( answer1 =1)   THEN 'answer1'
    WHEN ( answer2 =1)  THEN 'answer2'
    WHEN ( answer3 =1)   THEN ' answer3'
    ELSE 'nobody'
END
AS selected_answer
FROM Q
WHERE  Q.userid=1 AND (Q.answer1=1 OR Q.answer2=1 OR Q.answer3=1)

10-06 08:28