你好,我需要有关MYSQL查询的帮助
我有桌子
id | tn | title | customer_id |create_time | comment |
1 | 1342 | sample1 | customer1 | 2012-01-01 | hello world |
2 | 1342 | sample1 | customer1 | 2012-01-02 | hello world |
3 | 1342 | sample1 | customer1 | 2012-01-03 | hello new world |
4 | 3362 | sample2 | customer1 | 2012-01-02 | good bye world |
5 | 3362 | sample2 | customer1 | 2012-01-03 | good bye world |
6 | 3362 | sample2 | customer1 | 2012-01-04 | good bye world |
7 | 3362 | sample2 | customer1 | 2012-01-05 | good bye new world |
当我分组时
1 | 1342 | sample1 | customer1 | 2012-01-01 | hello world |
4 | 3362 | sample2 | customer1 | 2012-01-02 | good bye world |
但我需要
3 | 1342 | sample1 | customer1 | 2012-01-03 | hello new world |
7 | 3362 | sample2 | customer1 | 2012-01-05 | good bye new world |
这就像用最大ID或最大创建时间的TN分组。
我该怎么做?谢谢!
最佳答案
SELECT t2.* FROM
(SELECT MAX(id) AS id,tn FROM my_table GROUP BY tn) AS t1
LEFT JOIN my_table AS t2 USING(id)