gpt4 book ai didi

MySql Order by 未显示正确结果

转载 作者:行者123 更新时间:2023-11-29 07:26:13 25 4
gpt4 key购买 nike

我有 3 个表项目、讨论和评论。项目有多个讨论,每个讨论有多个评论。

我想将最后一个讨论添加到没有评论的讨论表中。我还需要该特定讨论所链接到的项目详细信息。

我的查询如下:
SELECT     p.PRO_Id, p.PRO_Name,    d.DIS_ThreadDesc, dev.DEV_Name, loc.LOC_Name, d.USE_Id, d.DIS_Date
FROM projects p LEFT JOIN discussions d ON p.PRO_Id = d.PRO_Id LEFT JOIN developer dev ON p.DEV_Id=dev.DEV_Id LEFT JOIN locality loc ON p.LOC_Id=loc.LOC_Id WHERE p.PRO_Status=1 and d.DIS_Status=1 AND d.DIS_Id NOT IN ( select DIS_Id from comments where COM_Status=2 ) GROUP BY p.PRO_Id ORDER BY d.DIS_Id desc LIMIT 3

我按降序对讨论表进行排序(ORDER BY d.DIS_Id),但它仍然采用第一条记录而不是最后一条记录。我在这里做错了什么?

最佳答案

试试这个:

SELECT p.PRO_Id, p.PRO_Name, d.DIS_ThreadDesc, dev.DEV_Name, 
loc.LOC_Name, d.USE_Id, d.DIS_Date
FROM projects p
INNER JOIN discussions d ON p.PRO_Id = d.PRO_Id
INNER JOIN (SELECT d.PRO_Id, MAX(d.DIS_Id) lastDisc
FROM discussions d
WHERE d.DIS_Status = 1
GROUP BY d.PRO_Id
) AS A ON d.PRO_Id = A.PRO_Id AND d.DIS_Id = A.lastDisc
LEFT OUTER JOIN comments c ON d.DIS_Id = c.DIS_Id AND c.COM_Status=2
LEFT OUTER JOIN developer dev ON p.DEV_Id = dev.DEV_Id
LEFT OUTER JOIN locality loc ON p.LOC_Id = loc.LOC_Id
WHERE p.PRO_Status = 1 AND c.DIS_Id IS NULL
ORDER BY d.DIS_Id DESC;

关于MySql Order by 未显示正确结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450809/

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