且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何根据SQL中的双条件选择行上下10条记录

更新时间:2023-01-28 19:52:19

在 SQL 中,您可以使用 union all 来做到这一点,首先得到分数较高或相等的 11 行,然后分数较低的 10 行:

In SQL, you can do this with a union all, first to get the 11 rows with higher or equal scores and then the 10 rows with lower scores:

(select s.*
 from scores s
 where s.score >= (select max(score) from scores where user = $USER)
 order by s.score asc
 limit 11
)
union all
(select s.*
 from scores s
 where s.score <= (select max(score) from scores where user = $USER) and users <> $USER
 order by s.score desc
 limit 10
)