sql - 如何在具有特定 ID 的记录之前和之后获得 5 条记录?

标签 sql

我有一张名为 scores 的表与列 idscore .我想通过其 id 访问特定记录以及它前后的5条记录。 SQL中有没有办法说“在它之前和之后获取带有nx项的id的分数?”

最佳答案

尝试:

  SELECT * 
    FROM scores 
   WHERE score >= n
ORDER BY score ASC
   LIMIT 6

 UNION

  SELECT * 
    FROM scores 
   WHERE score < n
ORDER BY score DESC
   LIMIT 5

语法可能会有所不同,具体取决于您使用的数据库服务器。

关于sql - 如何在具有特定 ID 的记录之前和之后获得 5 条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2459328/

相关文章:

java - 显示 NumberFormatException

sql - PostgreSQL age() 函数 : different/unexpected results when landing in dfferent month

mysql - 预订 : I want to display all records with a condition including if the record is null in table number 2

php - 使用 PHP 从 MYSQL 数据库格式化 DATETIME

sql - T-SQL View 在一次检索中检索两个查找

sql - Rails Active Record 查询 : answered questions, 按答案日期排序,不重复

mysql - bash 脚本中的 SQL 查询问题

sql - 工资高于部门平均水平的员工?

php - 在准备好的语句中使用变量两次

Mysql - 查找仅由两个用户进行的对话