我有一个问题,我在逻辑上不是很好,所以我试图只使用查询来做到这一点,
我有一个数据列表,我需要得到他们的排名,但问题是,我需要让前两个排名靠前,后两个排名靠后。
例如
id| name | score
1 bob 20
2 anna 10
3 jose 30
4 boni 30
5 lea 100
6 leo 10
7 qwertina 90
8 josh 50
9 king 40
10 queen 10假设我作为用户的id值是7。
所以如果我登录了,我的id是7
我需要获取
id| name | score
5 lea 100
6 leo 10
7 qwertina 90
8 josh 50
9 king 40这在mysql查询中是可能的吗?任何帮助都会非常感谢,我真的被这个问题卡住了。
发布于 2020-02-11 19:01:17
你似乎把你的逻辑建立在id的基础上,这很奇怪,但我们在这里:
SELECT id, name, score
FROM yourTable
WHERE id BETWEEN yourId-2 AND yourId+2发布于 2020-02-11 19:17:06
不确定只有MySQL的解决方案,但在伪PHP代码中
Grab records ordered by score
set a counter at 0
for loop starting 0, <= count of results
if the id your id?
if the loop index < 3 ? // there aren't two people ahead of you
return rows 0 to 4
elseif the loop index > count -3 ? // you are one of the last
return rows count -4 to count
else
return rows index -2 to index + 2 // you have 2 above and below you试着这样做,如果你在PHP上遇到困难
https://stackoverflow.com/questions/60167331
复制相似问题