0

我需要知道,我如何在飞行中做到这一点,

例如,我的客户处于不同的截止日期状态,我想选择 MAX(最近的截止日期)ON LEFT JOIN,当它加入两个表时,它选择了最旧的截止日期,这不是我想要的..

SELECT c.customerid, i.datedue
                        FROM  customers c
                               LEFT JOIN invoice i 
                                 ON i.customerid = c.customerid
                        WHERE  i.datedue <= UNIX_TIMESTAMP()
                               AND c.status!='d'
                        GROUP  BY i.customerid
                        ORDER BY i.datedue DESC
                        LIMIT  0, 1000
4

1 回答 1

2

您需要使用 max() 函数:

SELECT c.customerid, MAX(i.datedue)
FROM customers c LEFT JOIN invoice i ON i.customerid = c.customerid
WHERE i.datedue <= UNIX_TIMESTAMP() and c.status!='d'
GROUP BY i.customerid
ORDER BY i.datedue DESC
LIMIT 0,1000

这将为您提供每位客户的最长到期日期。

于 2012-04-24T04:40:30.957 回答