doudao1922 2018-08-15 15:26
浏览 32

PHP生成的查询显示错误在phpmyadmin中

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

The above query can run and give me the required result as i want but in phpmyadmin only

the question is this that when i put this query in my php code (codeigniter) and the model generated query is not running i don't know what to do please help me

PHP code

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();}

This is php generated Query

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

When i run the php generated query it didn't give me the required result as the first query mentioned

Error is given below

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (invoice) JOIN student ON invoice.studentID = student.studentID' at line 2

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)