这段mysql为什么运行不了:SELECT p.id, p.identifier,p.company,p.company_order,p.business_source,p.project_name,p.salesmen,p.charge,p.contract_sign_date,p.total_money, SUM(IFNULL(ir.invoice_money,0)) invoice_money, p.total_money-SUM(IFNULL(ir.invoice_money,0)) invoice_money_notready, (SUM(IFNULL(ir.invoice_money,0))/p.total_money) invoice_money_rate FROM `ly_project` p LEFT JOIN `ly_invoice_record` ir ON p.id = ir.project_id WHERE (SUM(IFNULL(ir.invoice_money,0))/p.total_money)<1 AND ir.delete_flag = 0 OR ir.delete_flag is NULL GROUP BY p.id
时间: 2024-04-03 07:36:18 浏览: 71
这段 MySQL 语句可能无法运行是因为SELECT语句中使用了SUM函数,而没有对LEFT JOIN中的表进行分组。在MySQL中,使用LEFT JOIN时,如果要对连接后的结果进行聚合操作,必须对LEFT JOIN的每个表都进行分组操作。因此,你需要将分组语句加入到LEFT JOIN语句中,例如:
SELECT p.id, p.identifier,p.company,p.company_order,p.business_source,p.project_name,p.salesmen,p.charge,p.contract_sign_date,p.total_money, SUM(IFNULL(ir.invoice_money,0)) invoice_money, p.total_money-SUM(IFNULL(ir.invoice_money,0)) invoice_money_notready, (SUM(IFNULL(ir.invoice_money,0))/p.total_money) invoice_money_rate
FROM `ly_project` p
LEFT JOIN `ly_invoice_record` ir
ON p.id = ir.project_id
WHERE ir.delete_flag = 0 OR ir.delete_flag is NULL
GROUP BY p.id, p.identifier,p.company,p.company_order,p.business_source,p.project_name,p.salesmen,p.charge,p.contract_sign_date,p.total_money
HAVING (SUM(IFNULL(ir.invoice_money,0))/p.total_money)<1
阅读全文