gpt4 book ai didi

SQL - ROW_NUMBER () OVER (ORDER BY) 不起作用

转载 作者:行者123 更新时间:2023-12-01 10:06:30 25 4
gpt4 key购买 nike

我在 ORDER BY 中放了什么无关紧要子句,结果集的顺序不会改变。

WITH Results AS (
SELECT DISTINCT
MessageThreadUsers.threadFK,
MessageThreads.threadDate,
Messages.MessageBody,
Messages.senderFK,
Users.userFullName AS senderFullName,
ROW_NUMBER() OVER (ORDER BY MessageThreads.threadDate DESC) AS RowNumber

FROM MessageThreadUsers
JOIN MessageThreads ON MessageThreadUsers.threadFK = MessageThreads.threadID
JOIN Messages ON MessageThreads.threadDate = Messages.messageDate
JOIN Users ON Messages.senderFK = Users.userID

WHERE userFK = 'usr_developer'
)
SELECT * FROM Results WHERE RowNumber BETWEEN 1 AND 10

最佳答案

ORDER BY 仅在应用于最外层 SELECT 语句时控制返回行的顺序。

这可能效果更好:

WITH Results AS (
SELECT DISTINCT
MessageThreadUsers.threadFK,
MessageThreads.threadDate,
Messages.MessageBody,
Messages.senderFK,
Users.userFullName AS senderFullName,
ROW_NUMBER() OVER (ORDER BY MessageThreads.threadDate DESC) AS RowNumber

FROM MessageThreadUsers
JOIN MessageThreads ON MessageThreadUsers.threadFK = MessageThreads.threadID
JOIN Messages ON MessageThreads.threadDate = Messages.messageDate
JOIN Users ON Messages.senderFK = Users.userID

WHERE userFK = 'usr_developer'
)
SELECT * FROM Results WHERE RowNumber BETWEEN 1 AND 10
ORDER BY RowNumber

关于SQL - ROW_NUMBER () OVER (ORDER BY) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9936911/

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