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

7kjnsjlb  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(293)

我尝试加入两个查询来比较使用代码点火器(ci)的学生两个学期的结果。
我可以直接用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中实现这一点?

31moq8wy

31moq8wy1#

试试这个,希望有用。

$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");

相关问题