我有一个形状的问题数据库
ID , Question , Answer1 , Answer2 , Answer3 , Answer4 , True
其中“True”是正确答案的索引:
True = 0 --> Answer1
....
True = 3 --> Answer4
我只需选择问题和正确的答案,这是我到目前为止所得到的:
SELECT Question , 'Answer' || (True+1) From Questions
但它返回问题和'answer1'或'answer2'...即:列的名称我不需要它的值
答案 0 :(得分:1)
你可以在MySQL中使用CASE
。
SELECT Question,
CASE True
WHEN 0 THEN Answer1
WHEN 1 THEN Answer2
WHEN 2 THEN Answer3
WHEN 3 THEN Answer4
END AS Answer
FROM tableName
OR
SELECT Question,
CASE
WHEN TRUE = 0 THEN Answer1
WHEN TRUE = 1 THEN Answer2
WHEN TRUE = 2 THEN Answer3
WHEN TRUE = 3 THEN Answer4
END AS Answer
FROM tableName
答案 1 :(得分:0)
如果您可以重构数据库架构,可以将其更改为:
QuestionID,QuestionText,AnswerID,AnswerText,IsCorrect
要获得正确答案,请使用此查询:
从QuestionID = X和IsCorrect = True
的问题中选择*要获得所有答案,请使用此查询:
从QuestionID = X
的问题中选择*现在,您可以根据需要获得尽可能多的答案。