简体   繁体   English

如何获得所有记录甚至那些没有匹配连接的记录 - MySQL

[英]How to get all records even those without matching join - MySQL

I have the following SQL which is getting all of the customer records and then finding the matching documents for that customer (points_audit_customers.id = points_audit_documents._audit_id) and it works however it is skipping all customers that do not have a document to match which was fine originally but now the spec has changed and I need all customers to come though, if no document is found then still show the customer details but leave the document fields from the sql as zero or null. 我有以下SQL获取所有客户记录,然后找到该客户的匹配文档(points_audit_customers.id = points_audit_documents._audit_id),但它可以正常跳过所有没有匹配文档的客户最初很好,但现在规格已经改变,我需要所有客户来,但如果没有找到文件,那么仍然显示客户的详细信息,但保留sql中的文档字段为零或null。

Is this possible? 这可能吗? I was wondering if somehow CASE could be used? 我想知道是否可以使用CASE? I guess if it isn't possible I can modify the SQL to only select customer details then in the PHP loop select the matching document, ideally I would prefer to do it with the SQL statment if I can. 我想如果不可能我可以修改SQL只选择客户的详细信息然后在PHP循环中选择匹配的文档,理想情况下,如果可以,我更愿意使用SQL语句。

Thankyou 谢谢

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

You need to use an outer join : 您需要使用外部联接

FROM  points_audit_customers LEFT JOIN points_audit_documents
        ON points_audit_customers.id = points_audit_documents.audit_id
WHERE import_month = '$import_month'
  AND processed = 1  -- if this is in documents table, move to the join criteria
SELECT pac.*, pad.branch,
    IFNULL(SUM(pad.amount_invoiced), 0) AS the_amount_invoiced,
    IFNULL(SUM(pad.amount_spent), 0)    AS the_amount_spent,    
    IFNULL(SUM(pad.points_invoiced), 0) AS invoiced_points,  
    IFNULL(SUM(pad.points_spent), 0)    AS spent_points,
    IFNULL(SUM(pad.points_bonus), 0)    AS bonus_points
FROM points_audit_customers pac 
LEFT JOIN points_audit_documents pad ON ( pad.id = pac.audit_id )
WHERE processed = 1
    AND import_month = '$import_month'
GROUP BY pac.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mysql表联接来自2个表的记录,但获得所有记录,即使是具有空字段的记录 - Mysql table join records from 2 tables but get all records even the ones with empty fields 如何使用php从mysql数据库中获取所有匹配的记录? - how to get all matching records from mysql database using php? 加入mysql表并在为空时获取所有记录 - join mysql table and get all records if empty 从 mysql JOIN 获取所有结果,即使没有连接匹配 - Get all results from mysql JOIN, even if no join matches PHP,MySQL连接两个表并显示具有和不具有关系的所有记录 - Php, MySql Join two tables and show all records with and without relationship 如何使用存储在数组中的php从MySql数据库中获取所有匹配记录的尊重? - How to get all matching records respectievelijk from MySql database using php which is stored in an array? MySQL JOIN->即使没有匹配项,也从右表中获取所有数据 - MySQL JOIN -> Get all data from right table even if there is no match 如何在mysql中获取所有类似的命名记录 - How to get all similar named records in mysql MySQL WEEK():获取日期范围内的所有周数(有/无记录) - MySQL WEEK() : Get all weeks in date range (with/without records) 选择所有匹配的两个字段,并使用mysql将这两个字段分组 - SELECT all matching two fields and grouping by those two fields with mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM