I know the title sounds like the a common question, but I have looked around here and can't seem to find problems (with answers) the same as mine.
So this is my case: I have three tables (major_criteria, questions, and results) ... There are multiple major criteria, and one major criteria has multiple questions. The table results holds all the users' answers to those questions.. Using this code:
$this->db->simple_query('set session group_concat_max_len=1000000');
$this->db->select('*, GROUP_CONCAT(CONCAT("<tr><td>", eq_question, "</td><td id=fix>", r_value) SEPARATOR "</td></tr>") as quesrate', false);
$this->db->from('evaluation_result');
$this->db->join('evaluation_question', 'evaluation_question.eq_ai=evaluation_result.er_eq_ai', left);
$this->db->join('evaluation_major_criteria', 'evaluation_major_criteria.emc_ai=evaluation_question.eq_emc_ai', left);
$this->db->join('rating', 'rating.r_ai=evaluation_result.er_rate', left);
$this->db->where('er_il_ai', $inst);
$this->db->where('er_ay_ai', $aca);
$this->db->where('er_sem_ai', $sem);
$this->db->where('er_et_ai', $evaltype);
$this->db->order_by('emc_code', 'asc');
$this->db->group_by('emc_code', 'asc');
$query=$this->db->get();
Assuming I have three users who answered, I was able to achieve this:
A. Major Criteria 1
Question 1 | Answer 1
Question 1 | Answer 2
Question 1 | Answer 3
Question 2 | Answer 1
Question 2 | Answer 2
Question 2 | Answer 3
B. Major Criteria 2
Question 1 | Answer 1
Question 1 | Answer 2
Question 1 | Answer 3
Question 2 | Answer 1
Question 2 | Answer 2
Question 2 | Answer 3
...
I was successfully able to group them by major criteria. But It's close to what I want to achieve. What I want is to group it by question too, so it doesn't repeat by how many the users have answered.
Something like this:
A. Major Criteria 1
Question 1 | Answer 1, Answer 2, Answer 3
Question 2 | Answer 1, Answer 2, Answer 3
B. Major Criteria 2
Question 1 | Answer 1, Answer 2, Answer 3
Question 2 | Answer 1, Answer 2, Answer 3
...
I have tried using two group_by and it doesn't work like how I want it to, according to this: Using group by on multiple columns
I have tried putting DISTINCT in GROUP_CONCAT but it gives me really weird combination of questions (some still repeated) under correctly grouped major criteria.
I have tried nested select statements with one or two group_by.
Idk. Maybe I'm looking at the wrong direction?
I have ran out of resources. I've looked around and tried everything I found, still no luck. This is my last resort. I'm using codeigniter for this, btw.
EDIT:
I also have this code from when I first made the page:
$this->db->select('*', false);
$this->db->from('evaluation_result');
$this->db->join('evaluation_question', 'evaluation_question.eq_ai=evaluation_result.er_eq_ai', left);
$this->db->join('evaluation_major_criteria', 'evaluation_major_criteria.emc_ai=evaluation_question.eq_emc_ai', left);
$this->db->join('rating', 'rating.r_ai=evaluation_result.er_rate', left);
$this->db->where('er_il_ai', $inst);
$this->db->where('er_ay_ai', $aca);
$this->db->where('er_sem_ai', $sem);
$this->db->where('er_et_ai', $evaltype);
$this->db->where('eq_s_ai', 2);
$this->db->order_by('emc_code', 'asc');
$this->db->group_by('eq_ai');
$query=$this->db->get();
And it groups by question. Now I have two queries, one that groups by major criteria, and one that groups by question. I guess I just have to combine them? But idk how.