获取每个不同客户ID的最后一列

时间:2015-11-14 01:55:43

标签: mysql

我有一张包含发票日期和客户ID的表格(当然还包括其他数据)。我希望在每个客户的表格中显示将来最长的发票日期。

我目前在WHERE中使用这个MySQL:

WHERE o70vm_invoices_invoices.invoice_date = (SELECT MAX(o70vm_invoices_invoices.invoice_date) FROM o70vm_invoices_invoices)";

此代码目前仅为一位客户显示列中最早的日期。

我在下面包含了我的整个MySQL查询,请记住,我必须从各种表中提取以显示我需要的数据。

$query = "SELECT       
    o70vm_invoices_contacts.name AS 'Parent_Name',
    o70vm_invoices_contacts.id AS 'Parent_ID',
    o70vm_invoices_contacts.company AS 'Children',
    o70vm_invoices_contacts.active AS 'Active',

    o70vm_invoices_invoices.id AS 'Invoice_ID',
    o70vm_invoices_invoices.user_id AS 'Parent_ID_on_Invoice',
    o70vm_invoices_invoices.invoice_num AS 'Invoice_Num',
    o70vm_invoices_invoices.invoice_date AS 'Invoice_Date',
    o70vm_invoices_invoices.invoice_duedate AS 'Invoice_Date_Due',

    o70vm_invoices_payments.invoice_id,
    o70vm_invoices_payments.id,
    o70vm_invoices_payments.payment_amount AS 'Payment_Amount',
    o70vm_invoices_payments.payment_datetime AS 'Payment_Date',
    o70vm_invoices_payments.payment_duedate,
    o70vm_invoices_payments.payment_status AS 'Payment_Status',
    o70vm_invoices_payments.payment_description AS 'Payment_Descript',
    o70vm_invoices_payments.payment_type AS 'Payment_Type'

    FROM o70vm_invoices_invoices, o70vm_invoices_contacts, o70vm_invoices_payments
    WHERE
    o70vm_invoices_contacts.id = o70vm_invoices_invoices.user_id
    AND
    o70vm_invoices_payments.invoice_id = o70vm_invoices_invoices.id
    AND
    o70vm_invoices_invoices.invoice_date < '2099-01-01'
    AND
    o70vm_invoices_contacts.active = 1
    AND o70vm_invoices_invoices.invoice_date = (SELECT MAX(o70vm_invoices_invoices.invoice_date) FROM o70vm_invoices_invoices)";

如果我使用GROUP BY:

GROUP BY o70vm_invoices_contacts.id

我可以拉出每个客户进行展示,但不会在表格中显示最后一张发票。

根据下面的Stefan评论,这是我修改后的子查询:

$query = "SELECT   *
FROM   ( SELECT 
    o70vm_invoices_contacts.name AS 'Parent_Name',
    o70vm_invoices_contacts.id AS 'Parent_ID',
    o70vm_invoices_contacts.company AS 'Children',
    o70vm_invoices_contacts.active AS 'Active',

    o70vm_invoices_invoices.id AS 'Invoice_ID2',
    o70vm_invoices_invoices.user_id AS 'Parent_ID_on_Invoice',
    o70vm_invoices_invoices.invoice_num AS 'Invoice_Num',
    o70vm_invoices_invoices.invoice_date AS 'Invoice_Date',
    o70vm_invoices_invoices.invoice_duedate AS 'Invoice_Date_Due',

    o70vm_invoices_payments.invoice_id,
    o70vm_invoices_payments.id,
    o70vm_invoices_payments.payment_amount AS 'Payment_Amount',
    o70vm_invoices_payments.payment_datetime AS 'Payment_Date',
    o70vm_invoices_payments.payment_duedate,
    o70vm_invoices_payments.payment_status AS 'Payment_Status',
    o70vm_invoices_payments.payment_description AS 'Payment_Descript',
    o70vm_invoices_payments.payment_type AS 'Payment_Type'

    FROM o70vm_invoices_invoices, o70vm_invoices_contacts, o70vm_invoices_payments
    WHERE
    o70vm_invoices_contacts.id = o70vm_invoices_invoices.user_id
    AND
    o70vm_invoices_payments.invoice_id = o70vm_invoices_invoices.id
    AND
    o70vm_invoices_invoices.invoice_date < '2099-01-01'
    AND
    o70vm_invoices_contacts.active = 1
    ORDER BY o70vm_invoices_invoices.invoice_date DESC

    ) AS tmp
    GROUP BY tmp.id";

此查询列出了第一个发票日期,而不是最后一个发票日期。如果我将订单更改为ASC,它只会对第一个发票日期的结果进行排序。

任何帮助,非常感谢。

2 个答案:

答案 0 :(得分:1)

最好的方法是使用子查询:

SELECT * 
FROM (
    SELECT * 
    FROM o70vm_invoices_invoices 
    ORDER BY invoice_date DESC
) AS tmp
GROUP BY tmp.id

答案 1 :(得分:0)

我确信有更好的方法可以做到这一点;但是,这很有效。

我刚刚在我的查询中添加了MAX:

max(o70vm_invoices_invoices.invoice_date) AS 'Invoice_Date',

,这列出了每个客户的最远日期。