我有以下SQL获取所有客户记录,然后找到该客户的匹配文档(points_audit_customers.id = points_audit_documents._audit_id),但它的工作原理它跳过所有没有文件匹配的客户,原本很好,但现在规范已经改变,我需要所有的客户来,如果没有找到文件,仍然显示客户的详细信息,但离开文件字段sql为零或null。如何获取所有记录,即使那些没有匹配的连接 - MySQL
这可能吗?我想知道是否可以使用CASE吗?我想如果这是不可能的,我可以修改SQL来只选择客户的详细信息,然后在PHP循环中选择匹配的文档,理想情况下,如果可以的话,我宁愿用SQL语句来完成。
三江源
SELECT points_audit_customers.*, points_audit_documents.branch,
SUM(points_audit_documents.amount_invoiced) AS the_amount_invoiced,
SUM(points_audit_documents.amount_spent) AS the_amount_spent,
SUM(points_audit_documents.points_invoiced) as invoiced_points,
SUM(points_audit_documents.points_spent) as spent_points,
SUM(points_audit_documents.points_bonus) as bonus_points
FROM points_audit_customers, points_audit_documents
WHERE import_month = '$import_month'
AND points_audit_customers.id = points_audit_documents.audit_id
AND processed = 1
感谢。这工作。 – sluggerdog 2013-04-30 01:05:33