gpt4 book ai didi

php - MYSQL 排序依据和分组依据

转载 作者:行者123 更新时间:2023-11-29 06:04:43 24 4
gpt4 key购买 nike

我目前有这个

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

但这仅按数据库中第一个找到的结果进行排序,因为这是我 SELECT id 时所采取的结果。我该如何做到这一点,以便从 notifications 中获取最后找到的 ID 以在 SELECT id 中使用?

最佳答案

只需选择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

关于php - MYSQL 排序依据和分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com