0

我正在尝试获取具有指定“拉”值的最新记录,然后按 imei 号分组。但是我得到了错误的输出,时间与我的数据库中的时间不匹配

这是我的表。

    IMEI              TIME                      STATUS
    354851057191551   2013-06-25 10:11:30       pull getrestbyimei
    354851057191551   2013-06-25 10:10:36       Push getrestbyimei
    354851057234971   2013-06-25 10:10:34       Push getbookings
    351895053436612   2013-06-25 10:10:32       pull getrestbyimei
    351895053436612   2013-06-25 10:10:31       pull getrestbyimei

期望的输出:

        IMEI              TIME                      STATUS
        354851057191551   2013-06-25 10:11:30       pull getrestbyimei
        351895053436612   2013-06-25 10:10:32       pull getrestbyimei

这就是我尝试的方式:

SELECT imei as IMEI, MAX(FROM_UNIXTIME(timestamp)) as TIME, action as STATUS
    FROM testlog
    where action like  'pull%'
    GROUP BY imei
order by TIME asc
4

5 回答 5

1

我认为您不需要该FROM_UNIXTIME功能。

SELECT imei as IMEI, MAX(timestamp) as TIME, action as STATUS
    FROM testlog
    where action like  'pull%'
    GROUP BY imei
order by TIME asc

这是SQL Fiddle 演示

于 2013-06-25T08:46:30.580 回答
1

您需要order by TIME desc检索最新记录

于 2013-06-25T08:40:15.473 回答
1

您需要一个子选择来获取每种类型的最新记录时间戳,然后将其连接回表以获取其余的行列。否则,您获得的操作可能是也可能不是最新的操作(如果您有多个不同的操作,但以拉取开始)。

SELECT a.imei, a.timestamp, a.action
FROM testlog a
INNER JOIN
(
    SELECT imei as IMEI, MAX(timestamp) as MaxTime
    FROM testlog
    where action like  'pull%'
    GROUP BY imei
) b
ON a.imei = b.imei
AND a.timestamp = b.MaxTime

另一个问题是您的时间戳是时间戳,而不是 unix 时间戳。因此,您不应将其从 unix 时间戳转换。

于 2013-06-25T08:53:45.983 回答
0

您可以使用 ORDER BY PRIMARY_KEY DESC

这里 PRIMARY_KEY 是您的主键字段

于 2013-06-25T08:41:58.073 回答
0

尝试这个

   SELECT imei as IMEI, MAX(timestamp) as TIME, action as STATUS
   FROM testlog
   where action like  'pull%'
   GROUP BY imei
   order by TIME asc
于 2013-06-25T08:54:46.943 回答