个人客户ID的最大日期

时间:2014-07-15 12:45:11

标签: php mysql sql date

我需要个人客户的最长日期。在具有唯一客户ID的表中有1个客户超过1次,这意味着客户在表中重复。我想要他们最新约会。

这是查询

SELECT  sum(receipt_table.installment_amount) total_installment,
        receipt_table.customer_id, 
        customer_info.name,
        customer_info.mobile,
        customer_info.plot_number,
        customer_info.amount, 
        max(receipt_table.date_time) last_date 
FROM    receipt_table,customer_info 
WHERE   customer_info.installment_type = 'monthly-12'
AND     customer_info.customer_id = receipt_table.customer_id 
GROUP BY customer_id 
HAVING  (customer_info.amount - sum(receipt_table.installment_amount) != 0) 

表格结构(receipt_table)

customer_id     receipt_id  installment_amount  date_time       
 41                115         1000              2014-07-27
 42                 116        1000              2014-07-27
 41                 117        1000              2014-07-30
 42                 118        1000              2014-07-30   

我的个人客户的最长日期是2014-07-27

1 个答案:

答案 0 :(得分:-1)

Try this instead of group by id group by name:
SELECT  sum(receipt_table.installment_amount) total_installment,receipt_table.  
customer_info.name, max(receipt_table.date_time)last_date FROM 
receipt_table,customer_info where customer_info.installment_type='monthly-12'and      
customer_info.customer_id=receipt_table.customer_id GROUP BY customer_name