user_id payment_end_time
1674 2008-01-10 07:51:21
1675 2008-01-05 17:48:51
1675 2009-01-20 16:56:04
1675 2010-01-27 13:16:34
1676 2008-01-05 20:21:35
1676 2009-01-07 18:46:41
1676 2009-12-08 13:59:08
1677 2008-01-05 20:30:16
1677 2009-01-03 19:11:03
1677 2009-10-07 06:24:58
1678 2008-01-26 06:23:46
1678 2009-01-18 12:19:58
1679 2008-01-06 12:11:29
1679 2009-01-23 08:44:51
1680 2008-03-28 18:58:20
1680 2009-12-31 16:39:46
1681 2008-01-07 07:24:03
1682 2008-01-07 11:37:43
1682 2009-01-16 20:26:16
1682 2010-02-02 09:38:34
1. user_id不是唯一的,因为用户可以多付一次。给定用户的每笔付款都会插入一个新行。
2.我必须使用数据库user_id(非主键)和payment_end_time中的字段。
3.我希望每个user_id的输出都带有payment_end_time(每个用户的最新日期时间,即按DESC排序)。
4.我想为每个user_id只想一次,即user_id(1675 2010-01-27 13:16:34) (1676 2009-12-08 13:59:08)同样适用于1677等等。
我试过这个
SELECT user_id,MAX(payment_end_time)
来自tbl_payment_info
GROUP BY user_id
ORDER BY payment_end_time DESC
限制1
我试过这个,但它只显示我的单行。我希望每个USER_id只使用最新的日期时间
答案 0 :(得分:3)
你走了:
SELECT user_id, MAX(payment_end_time) latest_payment_end_time
FROM mytable
GROUP BY user_id
ORDER BY latest_payment_end_time DESC
答案 1 :(得分:0)
SELECT * FROM (SELECT * FROM `payment` ORDER BY payment_end_time DESC) AS `p` GROUP BY user_id