Answer the question
In order to leave comments, you need to log in
How to implement a selection of correctly answered questions in mysql?
There are two tables, a table of answers to questions, and a table of user answers to these questions.
How to select all questions to which the user answered correctly, taking into account the fact that one question may have several correct answers.
I did not give a table of questions, since in the context of the task it is not needed, the connection with it goes by question_id
CREATE TABLE `cms_testing_answer` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Идентификатор',
`question_id` int(11) NOT NULL DEFAULT '0' COMMENT 'Вопрос',
`status` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT 'Статус',
`sort` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Сортировка',
`title` text NOT NULL COMMENT 'Заголовок',
`mark` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT 'Оценка',
`is_correct` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT 'Флаг правильного ответа',
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1169 DEFAULT CHARSET=utf8;
CREATE TABLE `cms_testing_user_answer` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Идентификатор',
`session_id` int(11) NOT NULL DEFAULT 'Идентификатор сессии тестирования',
`question_id` int(11) NOT NULL DEFAULT 'Идентификатор вопроса',
`user_id` int(11) unsigned NOT NULL DEFAULT 'Идентификатор пользователя',
`test_id` int(11) NOT NULL DEFAULT 'Идентификатор тестирования',
`answer_id` int(11) NOT NULL DEFAULT 'Идентификатор ответа',
`created_at` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Дата создания',
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=133 DEFAULT CHARSET=utf8;
cms_testing_answer
id | question_id | ___title___ | is_correct
1 ______1___________ Ответ 1_______1
2 ______1___________ Ответ 2_______1
3 ______1___________ Ответ 3_______0
cms_testing_user_answer
id | question_id | answer_id
1 ______1___________ 1
2 ______1___________ 2
cms_testing_user_answer
id | question_id | answer_id
1 ______1___________ 1
2 ______1___________ 3
cms_testing_user_answer
id | question_id | answer_id
1 ______1___________ 1
cms_testing_user_answer
id | question_id | answer_id
1 ______1___________ 1
1 ______1___________ 3
SELECT
qanswer.question_id
FROM
cms_testing_answer qanswer
LEFT JOIN cms_testing_user_answer uanswer ON uanswer.answer_id = qanswer.id
WHERE qanswer.is_correct=1
GROUP BY qanswer.question_id
HAVING COUNT(qanswer.id)=COUNT(uanswer.id)
Answer the question
In order to leave comments, you need to log in
Learn to formulate TK correctly. What is considered the "correct answer" in case of multivariance?
- 9 answers out of 10 correct - is it "correct"?
- all 10 out of 10 correct
and 1 out of incorrect ones - "Is this" correct?
The
"correct" fingerprint:
SELECT question_id, GROUP_CONCAT(id ORDER BY sort ASC SEPARATOR '_')
FROM cms_testing_answer
WHERE is_correct = 1
GROUP BY question_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question