1

我有一个 MySQL 数据库,其中包含两个问题和答案表。每个问题都有一个正确答案和三个错误答案。每个问题总是有四个答案,只有一个是正确的。

这些表是:

CREATE TABLE `question` (
  `id_question` smallint(5) unsigned NOT NULL auto_increment,
  `text` varchar(255) collate utf8_unicode_ci default NULL,
  PRIMARY KEY  (`id_question`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `answer` (
  `id_answer` mediumint(8) unsigned NOT NULL auto_increment,
  `id_question` smallint(5) unsigned NOT NULL,
  `is_correct` tinyint(1) NOT NULL,
  `text` varchar(45) collate utf8_unicode_ci default NULL,
  PRIMARY KEY  (`id_answer`,`id_question`),
  KEY `fk_id_question_idx` (`id_question`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我需要有关选择查询的帮助。我想得到一个表格,其中行中的问题和列中的四个答案(第一个是正确的,然后是其他三个)。到目前为止,我能够得到这样的输出:

Question | Answer | Is_Correct
-------------------------------
Question 1   Answer 1-1   1
Question 1   Answer 1-2   0
Question 1   Answer 1-3   0
Question 1   Answer 1-4   0
Question 2   Answer 2-1   1
Question 2   Answer 2-2   0
Question 2   Answer 2-3   0
Question 2   Answer 2-4   0
...

我怎样才能得到以下结果?

Question | Correct_Answer | Incorrect_answer1 | Incorrect_answer2 | Incorrect_answer3
--------------------------------------------------------------
Question 1    Answer 1-1    Answer 1-2    Answer 1-3    Answer 1-4
Question 2    Answer 2-1    Answer 2-2    Answer 2-3    Answer 2-4
4

2 回答 2

2

您可以使用带有表达式的聚合函数来透视数据。CASE您可以使用用户定义的变量按问题在每行上实现行号。您的代码将与此类似:

select q.text Question,
  max(case when a.is_correct = 1 then a.text end) Correct_answer,
  max(case when a.is_correct = 0 and rn=1 then a.text end) Incorrect_Answer1,
  max(case when a.is_correct = 0 and rn=2 then a.text end) Incorrect_Answer2,
  max(case when a.is_correct = 0 and rn=3 then a.text end) Incorrect_Answer3
from question q
inner join
(
  select a.id_question,
    a.text,
    a.is_correct,
    a.id_answer,
    @row:=case 
            when @prevQ=id_question
              and is_correct = 0
            then @row +1 
            else 0 end  rn,
    @prevA:=id_answer,
    @prevQ:=id_question
  from answer a
  cross join (select @row:=0, @prevA:=0, @prevQ:=0)r
  order by a.id_question, a.id_answer
) a
  on q.id_question = a.id_question
group by q.text
order by a.id_question, a.id_answer

请参阅SQL Fiddle with Demo。这将结果显示在单独的列中:

|   QUESTION | CORRECT_ANSWER | INCORRECT_ANSWER1 | INCORRECT_ANSWER2 | INCORRECT_ANSWER3 |
-------------------------------------------------------------------------------------------
| Question 1 |     Answer 1-1 |        Answer 1-2 |        Answer 1-3 |        Answer 1-4 |
| Question 2 |     Answer 2-1 |        Answer 2-2 |        Answer 2-3 |        Answer 2-4 |
于 2013-03-07T19:54:24.020 回答
1

为此构建一个动态数据透视查询需要做很多工作。相反,我可能会做的是使用 MySQL 的GROUP_CONCAT()聚合函数来创建一个以逗号分隔的Incorrect_answer字段列表,同时将其分离Correct_Answer为自己的列:

SELECT
  question.`text`,
  /* Separate the one where Is_Correct = 1 and discard the others with a MAX() aggregate */
  MAX(CASE WHEN Is_Correct = 1 THEN answer.`text` ELSE NULL END) AS Correct_Answer,
  /* Group the rows where Is_Correct = 0 into a comma-separated list */
  GROUP_CONCAT(CASE WHEN Is_Correct = 0 THEN answer.`text` ELSE NULL END) AS Incorrect_answers
FROM
  question
  JOIN answer ON question.id_question = answer.id_question
GROUP BY Question.`text`

您的应用程序代码收到的结果如下所示:

Question     Correct_Answer Incorrect_answers
--------------------------------------------------------------
Question 1    Answer 1-1    Answer 1-2,Answer 1-3,Answer 1-4
Question 2    Answer 2-1    Answer 2-2,Answer 2-3,Answer 2-4

然后在您的应用程序代码中拆分Incorrect_answers列变得微不足道,,因为它是一个逗号分隔的列表。

例如,在 PHP 中,类似:

$incorrect = explode(',', $row['Incorrect_answers']);

或者在 Ruby 或 Python 中:

incorrect = incorrect_answers.split(',')
于 2013-03-07T19:34:57.863 回答