select student.studentID, invoice.invoiceID, student.name,
MONTHNAME(STR_TO_DATE(invoice.month, '%m')) as Month, invoice.tuitionfee as 'Total Amount', payment.tuitionpaid
from invoice
join student
left join payment
on invoice.studentID = student.studentID and invoice.invoiceID = payment.invoiceID
where invoice.`create_date` > DATE_SUB(now(), INTERVAL 6 MONTH) and student.studentID = '28' group by invoice.month
上面的查询可以运行,并根据需要提供给我所需的结果,但仅限于phpmyadmin
问题是,当我将此查询放入我的php代码(codeigniter)中并且模型生成的查询未运行时,我不知道该怎么办,请帮帮我
PHP代码
public function get_month_wise_dues($id){
$this->db->select("student.studentID,
invoice.invoiceID, student.name,
MONTHNAME(STR_TO_DATE(invoice.month, '%m')) as Month,
invoice.tuitionfee as 'Total Amount', payment.tuitionpaid");
$this->db->from('invoice');
$this->db->join('student','invoice.studentID = student.studentID');
$this->db->join('payment','invoice.invoiceID = payment.invoiceID','LEFT');
$this->db->where('invoice.`create_date` > DATE_SUB(now(), INTERVAL 6 MONTH) and student.studentID=', $id);
$this->db->group_by('invoice.month');
$this->db->order_by('invoice.month', 'asc');
$query = $this->db->get();
// echo $this->db->last_query();
// exit;
return $query->result();}
这是php生成的查询
SELECT `student`.`studentID`, `invoice`.`invoiceID`, `student`.`name`, MONTHNAME(STR_TO_DATE(invoice.month, `'%m'))` as Month, `invoice`.`tuitionfee` as 'Total Amount', `payment`.`tuitionpaid`
FROM (`invoice`)
JOIN `student` ON `invoice`.`studentID` = `student`.`studentID`
LEFT JOIN `payment` ON `invoice`.`invoiceID` = `payment`.`invoiceID`
WHERE `invoice`.`create_date` > DATE_SUB(now(), INTERVAL 6 MONTH) and
student.studentID= '28'
GROUP BY `invoice`.`month`
ORDER BY `invoice`.`month` asc
当我运行php生成的查询时,它没有给我所需的结果,因为提到了第一个查询
错误在下面给出
1064-您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册以使用正确的语法 在'FROM(
invoice
)附近student
上加入invoice
。studentID
=student
。studentID
”位于第2行