0

我目前有这个

SELECT type, extra_id, COUNT(*) AS count, id 
FROM `notifications` 
WHERE `receiver_id` = '".$this->user_id."' 
    AND `read` = '0' 
GROUP BY type, extra_id 
ORDER BY `id` DESC

但这仅按数据库中第一个找到的结果排序,因为这是我选择 id 时所采用的。我怎样才能做到这一点,以便notifications在 SELECT id中使用最后找到的 ID ?

4

1 回答 1

3

只需选择MAX(id)而不是id

SELECT type, extra_id, COUNT(*) AS count, MAX(id) AS max_id
FROM `notifications` 
WHERE `receiver_id` = '".$this->user_id."' 
    AND `read` = '0' 
GROUP BY type, extra_id 
ORDER BY max_id DESC
于 2012-08-22T20:19:23.537 回答