我有一个 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