使用JOIN查询从数据库中选择上一个时间戳

时间:2013-03-31 00:17:13

标签: php mysql

我有一个选择组和组发布的查询。在postss表中有一个时间戳collumn和一个group_id。我需要在发布表中选择与组ID相关联的最大时间戳。

这是我的查询

$query = "
    SELECT groups.group_name, groups.group_info, groups.group_tags, postings.timestamp 
    FROM groups 
    LEFT JOIN postings ON groups.group_id = postings.group_id 
    GROUP BY groups.group_id 
    ORDER BY `postings`.`timestamp` DESC
";

1 个答案:

答案 0 :(得分:0)

$query = "
    SELECT groups.group_name, groups.group_info, groups.group_tags, max(postings.timestamp) 
    FROM groups 
    LEFT JOIN postings ON groups.group_id = postings.group_id 
    GROUP BY groups.group_id 
    ORDER BY `postings`.`timestamp` DESC
";