用于多列分页的SQL查询;了解OR运算子

编程入门 行业动态 更新时间:2024-10-23 01:40:42
本文介绍了用于多列分页的SQL查询;了解OR运算子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用一个工具,该工具会在下面生成Postgresql查询:

I'm using a tool that generates the Postgresql query below:

SELECT "id", "score" FROM "players" WHERE "score" > '11266' OR ( "score" = '11266' AND "id" > '4482' ) ORDER BY "score" ASC, "id" ASC LIMIT 3

我需要了解为什么OR运算符?

I need to understand why the OR operator?

我的 players 表可以包含许多行,这些行具有相同的score但不相同的id.

My players table can have many rows with the same score but not the same id.

当多行具有相同的score值时是否需要OR?

Is that OR needed when multiple rows has the same score value?

推荐答案

OR的目的(如您所怀疑的)是处理分数中有联系的情况.想法是通过包含id来进行稳定的排序,因此这可以在之后 (score, id)中获得所有内容.

The purpose of the OR -- as you suspect -- is to handle the case where there are ties in the scores. The idea is to make a stable sort by including the id, so this this getting everything after (score, id).

大概,用于score和id的值是最后看到的值(可能在上一页,但这是推测).

Presumably, the values used for score and id are the last values seen (probably on the previous page, but that is speculation).

稳定"排序是一种每次应用时都以相同顺序返回行的排序.因为SQL表表示无序集,所以联系意味着不稳定排序.包含id使其稳定(假设id是唯一的.

A "stable" sort is one that returns the rows in the same order each time it is applied. Because SQL tables represent unordered sets, ties imply an unstable sort. Including the id makes it stable (assuming that id is unique.

Postgres实际上支持简单语法:

Postgres actually supports a simper syntax:

where (score, id) > (11266, 4482)

请注意,我也删除了单引号.这些值看起来像数字,因此应将它们视为数字而不是字符串.

Note that I also removed the single quotes. The values look like numbers so they should be treated as numbers not strings.

更多推荐

用于多列分页的SQL查询;了解OR运算子

本文发布于:2023-10-14 22:23:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!