我的数据库中有一张与以下表格相似的表格:
id | name | score
==========================
... ... ...
44 Bob 89
45 Jane 567
46 Andrew 22
... ... ...
score
将始终是一个正整数。假设我要返回5个分数最接近Jane的用户。假设5是变量,我将如何使用SQL(或Eloquent)做到这一点?
select * from your_table
order by abs(score - (select score from your_table where name = 'Jane'))
limit 5
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句