customer_i fundamt installment date_time
41 115 1054 2014-05-27
42 116 1067 2014-07-27
41 117 1089 2014-07-30
42 118 1120 2014-07-30
43 118 1120 2014-08-30
42 118 1120 2014-08-30
41 118 1120 2014-06-30
43 118 1120 2014-02-30
我想这样:
customer_id fundamt installment date_time
41 117 1089 2014-07-30
42 118 1120 2014-08-30
43 118 1120 2014-08-30
包含max date_time的行:
PLease帮助我在Mysql中编写SQL
答案 0 :(得分:1)
彻底解决后,你的问题实现了实际要求。这可能是简单的查询,更易于阅读和维护。
SELECT * FROM your_table WHERE (customer_id, date_time) IN ( SELECT customer_id, MAX(date_time) FROM test GROUP BY customer_id )
ORDER BY customer_id
修改强>
我能想到的其他选择就是将自己加入桌子。
SELECT a.*
FROM your_table a
LEFT JOIN your_table b ON a.customer_id = b.customer_id AND a.date_time < b.date_time
WHERE b.customer_id IS NULL
答案 1 :(得分:1)
您可以在子句中使用子选择
SELECT customer_id, fundamt, installment, date_time
FROM your_table
where ( customer_id, date_time) in (select customer_id , max(date_time)
FROM your_table
GROUP BY customer_id )