user1527354
user1527354

Reputation: 195

MYSQL Order by and Group by

I currently have this

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

But this only orders by the first found result in the database as that's what is taken when i SELECT id. how can i make this so it takes the last found ID from notifications to use in the SELECT id?

Upvotes: 0

Views: 63

Answers (1)

eggyal
eggyal

Reputation: 126035

Just select MAX(id) instead of 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

Upvotes: 3

Related Questions