Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用Codeigniter将两个不同的查询连接到一个查询中_Mysql_Codeigniter_Join_Compare_Subquery - Fatal编程技术网

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

Mysql 如何使用Codeigniter将两个不同的查询连接到一个查询中,mysql,codeigniter,join,compare,subquery,Mysql,Codeigniter,Join,Compare,Subquery,我试图加入两个查询,以比较使用CodeIgniter(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 st

我试图加入两个查询,以比较使用CodeIgniter(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中实现这一点

试试这个,希望它能起作用

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