MySQL查询字符串不正确

时间:2015-03-22 22:26:47

标签: mysql

我正在尝试使用MySQL获得某个结果集,但我不确定如何将其删除。我想要call_logs表中的所有,以获取timestamp每个calls_id的最新条目。这是我到目前为止所尝试的内容:

SELECT distinct calls_id FROM call_logs order by timestamp

但这只给了我calls_id。当我提出以下内容时:

SELECT distinct calls_id, created, modified, viewed FROM call_logs order by timestamp

它返回所有条目,而不是仅返回每个calls_id的最后一个条目。

3 个答案:

答案 0 :(得分:1)

使用子查询获取每个id的最大时间戳。然后加入原来的表格:

select cl.*
from call_logs cl join
     (select calls_id, max(timestamp) as maxts
      from call_logs
      group by calls_id
     ) ct
     on cl.calls_id = ct.calls_id and cl.timestamp = ct.maxts

答案 1 :(得分:0)

$ query =“SELECT * FROM(SELECT * FROM call_logs ORDER BY timestamp DESC)AS t1 GROUP BY calls_id”;

答案 2 :(得分:-1)

$query="select * from  call_logs group by calls_id order by timestamp"