MSSQL 分页在不应该的情况下返回随机行

发布于 2024-07-13 05:23:03 字数 1434 浏览 6 评论 0原文

我正在尝试在 MSSQL 中进行一些基本的分页。 我遇到的问题是,我正在对(可能)具有相似值的行进行分页排序,并且 ORDER BY 子句返回“随机”结果,这效果不佳。

例如。

如果我有三行,并且我按“评级”对它们进行排序,并且所有评级均为 = '5' - 这些行似乎会“随机”排序。 如何使行每次都以相同的顺序显示?

我尝试按该字段上次编辑的日期时间对其进行排序,但“评级”按相反顺序排序,并且再次无法按照我期望的方式工作。

这是我迄今为止使用的 SQL。 我知道如果没有数据,这有点令人困惑,所以……任何帮助都会非常有用。

SELECT * FROM 
(
  SELECT 
    CAST(grg.defaultthumbid AS VARCHAR) + '_' + 
    CAST(grg.garageid AS VARCHAR) AS imagename,
    (
      SELECT COUNT(imageid) 
      FROM dbo.images im (nolock) 
      WHERE im.garageid = grg.garageid
    ) AS piccount, 
    (
      SELECT COUNT(commentid) 
      FROM dbo.comments cmt (nolock) 
      WHERE cmt.garageid = grg.garageid
    ) AS commentcount,
    grg.GarageID, mk.make, mdl.model, grg.year, 
    typ.type, usr.username, grg.content, 
    grg.rating, grg.DateEdit as DateEdit,
    ROW_NUMBER() OVER (ORDER BY Rating DESC) As RowIndex 
  FROM 
    dbo.garage grg (nolock)
    LEFT JOIN dbo.users (nolock) AS usr ON (grg.userid = usr.userid)
    LEFT JOIN dbo.make (nolock) AS mk ON (grg.makeid = mk.makeid)
    LEFT JOIN dbo.type (nolock) AS typ ON (typ.typeid = mk.typeid)
    LEFT JOIN dbo.model (nolock) AS mdl ON (grg.modelid = mdl.modelid)
  WHERE 
    typ.type = 'Automobile' AND
    grg.defaultthumbid != 0 AND
    usr.username IS NOT NULL
) As QueryResults 
WHERE 
  RowIndex BETWEEN (2 - 1) * 25 + 2 AND 2 * 25
ORDER BY 
  DateEdit DESC

I'm trying to do some basic paging in MSSQL. The problem I'm having is that I'm sorting the paging on a row that (potentially) has similar values, and the ORDER BY clause is returning "random" results, which doesn't work well.

So for example.

If I have three rows, and I'm sorting them by a "rating", and all of the ratings are = '5' - the rows will seemingly "randomly" order themselves. How do I make it so the rows are showing up in the same order everytime?

I tried ordering it by a datetime that the field was last edited, but the "rating" is sorted in reverse, and again, does not work how i expect it to work.

Here is the SQL I'm using thus far. I know it's sort of confusing without the data so.. any help would be greatful.

SELECT * FROM 
(
  SELECT 
    CAST(grg.defaultthumbid AS VARCHAR) + '_' + 
    CAST(grg.garageid AS VARCHAR) AS imagename,
    (
      SELECT COUNT(imageid) 
      FROM dbo.images im (nolock) 
      WHERE im.garageid = grg.garageid
    ) AS piccount, 
    (
      SELECT COUNT(commentid) 
      FROM dbo.comments cmt (nolock) 
      WHERE cmt.garageid = grg.garageid
    ) AS commentcount,
    grg.GarageID, mk.make, mdl.model, grg.year, 
    typ.type, usr.username, grg.content, 
    grg.rating, grg.DateEdit as DateEdit,
    ROW_NUMBER() OVER (ORDER BY Rating DESC) As RowIndex 
  FROM 
    dbo.garage grg (nolock)
    LEFT JOIN dbo.users (nolock) AS usr ON (grg.userid = usr.userid)
    LEFT JOIN dbo.make (nolock) AS mk ON (grg.makeid = mk.makeid)
    LEFT JOIN dbo.type (nolock) AS typ ON (typ.typeid = mk.typeid)
    LEFT JOIN dbo.model (nolock) AS mdl ON (grg.modelid = mdl.modelid)
  WHERE 
    typ.type = 'Automobile' AND
    grg.defaultthumbid != 0 AND
    usr.username IS NOT NULL
) As QueryResults 
WHERE 
  RowIndex BETWEEN (2 - 1) * 25 + 2 AND 2 * 25
ORDER BY 
  DateEdit DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2024-07-20 05:23:03

尝试同时订购,例如:
按评级 DESC、日期编辑 ASC 排序

Try ordering by both, e.g.:
ORDER BY Rating DESC, DateEdit ASC

浊酒尽余欢 2024-07-20 05:23:03

该查询首先按 [Rating] 对行进行编号,然后按 [DateEdit] 对结果重新排序。 可能不是你想要的。 按 [RowIndex] ASC 排序应该可以解决这个问题。

ROW_NUMBER() OVER (ORDER BY [Rating] DESC) As [RowIndex]
...
ORDER BY [RowIndex]

The query first numbers the rows by [Rating], and then re-sorts the results by [DateEdit]. Possibly not what you intended. Ordering by [RowIndex] ASC should sort it out.

ROW_NUMBER() OVER (ORDER BY [Rating] DESC) As [RowIndex]
...
ORDER BY [RowIndex]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文