gpt4 book ai didi

php - 通过不给出真实结果来排序

转载 作者:行者123 更新时间:2023-12-01 00:18:37 28 4
gpt4 key购买 nike

你好,我试过这个查询:

SELECT 
*
FROM
(
SELECT
*
FROM
specials_companies_mails
WHERE
`toId` = 1 && parentId <> 0
ORDER BY
specials_companies_mails.mailId DESC
) AS b
GROUP BY
b.parentId

我在表中有两行:

mailID           subject        toId        parentId
3 test1 1 2
4 test2 1 2

以及查询后的结果:

mailID           subject        toId        parentId
3 test1 1 2

它的意思是

ORDER BY specials_companies_mails.mailId DESC

不工作...它给我 mailId 3 而不是 4...

怎么办?

非常感谢

最佳答案

GROUP BY 不关心 ORDER BY。所选记录将完全不确定。

你可以做的是为每个 parentId 找到最新的 mainId,如下所示:

select a.* 
from specials_companies_mails AS a
left join specials_companies_mails AS b
on a.parentId = b.parentId
and a.mailID < b.mailID
and b.toId = 1
and b.parentId <> 0
WHERE b.parentId is null
and a.toId = 1
and a.parentId <> 0

关于php - 通过不给出真实结果来排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132164/

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