0
   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) FROM tbl_payment_info
GROUP BY user_id ORDER BY payment_end_time DESC LIMIT 1

我已经尝试过了,但它只显示单行。我希望每个 USER_id 只使用一次最新的日期时间

4

2 回答 2

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
于 2013-04-08T13:34:58.240 回答
0
SELECT * FROM (SELECT * FROM `payment` ORDER BY payment_end_time DESC) AS `p` GROUP BY user_id
于 2013-04-08T13:38:49.677 回答