This project is based on trivia(Question/Answer Quiz) basically scenario its so simple but i have to improve/ more optimize my query using these tables
I have 5 different table.... (working Query)
$query=$this->db->query("SELECT
q.`id`,
q.`topic_id`,
t.`topic`,
q.`question`,
GROUP_CONCAT(a.`answer`) AS `answer`,
GROUP_CONCAT(a.`is_correct`) AS `is_correct`,
GROUP_CONCAT(a.`id`) AS `ans_id`,
q.`answer_type`,
q.`image_url`,
q.`created_date`,
(SELECT ua.`topic_id` FROM usersAnswer ua
WHERE t.`id`= ua.topic_id AND ua.`user_id` = '".$user_id."' LIMIT 1)
AS `userdone_topic`,
(SELECT ua.`answer_id`
FROM usersAnswer ua
WHERE ua.`question_id` = q.`id` AND ua.`user_id`='".$user_id."'
AND ua.`answer_id`
IN
(SELECT a.`id` FROM answers a
WHERE a.`is_correct` != '0' AND a.question_id = q.`id`
)) AS `user_correct_id`
FROM
questions q INNER JOIN topics t ON q.`topic_id`= t.`id`
INNER JOIN answers a ON q.`id`=a.`question_id`
WHERE
(SELECT s.`status`
FROM `status` s
WHERE
s.`month_date`= q.created_date AND s.`status`= 2
$valid)
GROUP BY q.`id`
");
return $query->result();