如何使用Codeigniter将两个不同的查询连接到一个查询中

时间:2018-05-30 11:47:04

标签: mysql codeigniter join compare subquery

我正在尝试加入两个查询,以便比较使用Code Igniter(CI)的学生2学期的结果。

我可以直接使用MySQL,但如何使用CI?

以下是MySQL的查询:

select x.student, x.2017, y.2018 from (SELECT a.student_id student, sum(a.marks) 2017 FROM a a 
WHERE a.year = '2017' GROUP by student
) x
LEFT JOIN
(SELECT b.student_id student, sum(b.marks) 2018 FROM b b
WHERE b.year = '2018' GROUP by student
) y on x.student = y.student

如何在CI中完成此操作?

1 个答案:

答案 0 :(得分:0)

试试这个,希望它有效。

    $this->db->query("select x.student, x.2017, y.2018 from (SELECT 
    a.student_id student, sum(a.marks) 2017 FROM a a 
    WHERE a.year = '2017' GROUP by student
    ) x
    LEFT JOIN
    (SELECT b.student_id student, sum(b.marks) 2018 FROM b b
    WHERE b.year = '2018' GROUP by student
    ) y on x.student = y.student");