E
E
Evgeny Bukharev2017-07-05 10:01:08
MySQL
Evgeny Bukharev, 2017-07-05 10:01:08

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;

An example of a correct / incorrect answer from the table
Answers to the question
cms_testing_answer
id | question_id | ___title___ | is_correct
1 ______1___________ Ответ 1_______1
2 ______1___________ Ответ 2_______1
3 ______1___________ Ответ 3_______0

The user answered the question correctly
cms_testing_user_answer
id | question_id | answer_id 
1 ______1___________ 1
2 ______1___________ 2

The user answered the question incorrectly
cms_testing_user_answer
id | question_id | answer_id 
1 ______1___________ 1
2 ______1___________ 3

The user answered the question incorrectly
cms_testing_user_answer
id | question_id | answer_id 
1 ______1___________ 1

The user answered the question incorrectly
cms_testing_user_answer
id | question_id | answer_id 
1 ______1___________ 1
1 ______1___________ 3

In general, we managed to make a correct request
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

1 answer(s)
B
Boris Korobkov, 2017-07-05
@BorisKorobkov

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

(you can use a different separator or binary shifts).
For optimization, this fingerprint can be stored in the database in cms_question so that it does not have to be repeated each time.
The user's response fingerprint is similar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question