CREATE TABLE questions (
_id INTEGER PRIMARY KEY AUTOINCREMENT
, question TEXT
, correct_answer TEXT DEFAULT (null)
, answer1 TEXT DEFAULT (null)
, answer2 TEXT DEFAULT (null)
, answer3 TEXT DEFAULT (null)
, answer4 TEXT DEFAULT (null)
, difficulty INTEGER DEFAULT (null)
, nid INTEGER
);
现在要省略correct_answer列,并在answer1列中做出正确答案,在answer2,answer3,answer4中做出其他3个错误答案。 但问题是目前正确答案可能是四个中的任何一个,所以我无法合并
作为示例表就像
你可以在第一行看到答案2中的正确答案,第二行正确答案是在答案4中
那我怎么只在answer1栏中给出正确答案,在答案2,答案3,答题4中分别给出其他错误答案?
什么是sql查询?或任何其他想法?
答案 0 :(得分:5)
我会做这样的事情:
insert into question_new (_id, qestion, answer1, answer2, answer3, answer4)
SELECT _id, question,
correct_answer as answer1,
if (correct_answer = answer2 , answer1 , answer2) as answer2,
if (correct_answer = answer3 , answer1 , answer3) as answer3,
if (correct_answer = answer4 , answer1 , answer4) as answer4
FROM questions
简单来说:
<强>更新强>
使用case when
insert into question_new (_id, qestion, answer1, answer2, answer3, answer4)
SELECT _id, question,
correct_answer as answer1,
case when correct_answer = answer2 then answer1 else answer2 end as answer2,
case when correct_answer = answer3 then answer1 else answer3 end as answer3,
case when correct_answer = answer4 then answer1 else answer4 end as answer4
FROM questions
答案 1 :(得分:4)
我会这样做
insert into questions2(_id, qestion, answer1, answer2, answer3, answer4)
select _id, question , correct_answer as answer1,
case when correct_answer = answer1 then answer2 else answer1 end answer2,
case when correct_answer = answer2 then answer3 else answer2 end answer3,
case when correct_answer = answer3 then answer4 else answer3 end answer4
from questions
因此,您最终会得到新表格,其中包含您所需格式的问题
答案 2 :(得分:4)
你可以试试这个 -
SELECT question, correct_answer,
CASE WHEN answer1 != correct_answer THEN answer1 ELSE answer2 END AS incorrect1,
CASE WHEN (answer1 != correct_answer AND answer2 != correct_answer) THEN answer2 ELSE answer3 END AS incorrect2,
CASE WHEN (answer1 != correct_answer AND answer2 != correct_answer AND answer3 != correct_answer) THEN answer3 ELSE answer4 END AS incorrect3
FROM questions
答案 3 :(得分:3)
有点晚了,但你可以更新你的表而不需要辅助表来存储你想要的数据
UPDATE questions
SET
answer2 = CASE WHEN answer2 = correct_answer
THEN answer1 ELSE answer2 END,
answer3 = CASE WHEN answer3 = correct_answer
THEN answer1 ELSE answer3 END,
answer4 = CASE WHEN answer4 = correct_answer
THEN answer1 ELSE answer4 END,
answer1 = correct_answer
CHECK 的 SQL FIDDLE HERE 强>