按月加入3个表组

时间:2018-08-15 14:50:44

标签: mysql codeigniter join left-join

我是Mysql的新手。我想根据数据库制作图表。我有3个表格:发票,购买和月份。我想加入所有人,并按月分组。 我已经加入2:发票和月份以及购买和月份。有效。但是当我尝试加入其中的3个时,出现了错误。 下面是我的代码。

function get_chart(){

    $query = $this->db->query("SELECT month.month_name as month, 
        SUM(table_purchase.subtotal) AS total1,
        SUM(table_invoice.subtotal) AS total2  
        FROM month 
        LEFT JOIN table_purchase ON (month.month_num = MONTH(table_purchase.date_pur)  
        LEFT JOIN table_invoice ON (month.month_num = MONTH(table_invoice.date_inv)

        GROUP BY month.month_name ORDER BY month.month_num");

    $res = array();

        foreach($query->result_array() as $data){
            $res[] = array(
                "month" => $data['month'],
                "total1" => $data['total1'],
                "total2" => $data['total2'],
            );
        }
        return $res;

}

错误: 您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第6行的“ LEFT JOIN table_invoice ON(month.month_num = MONTH(table_invoice.date_inv)”附近使用

1 个答案:

答案 0 :(得分:1)

查看您的代码,在ON子句的开头有一个错误的with(df, interaction(parent, child)) ..尝试删除

(