mysql 根据某个值叠加查询

今天看到需求

根据输入用户ID由近到远排列

怎么会有这种需求???

直接上代码

SELECT * FROM Member,
(
(SELECT ABS(ID-900) as Sorting,ID FROM Member WHERE ID>900 ORDER BY ID Asc LIMIT 0,20)
 union all --注意此
(SELECT  ABS(900-ID) as Sorting,ID FROM Member WHERE ID<900 ORDER BY ID Desc LIMIT 0,20)
ORDER BY Sorting    --取得ID为880-920的行  并合并为一张表,同列名合并为一列,然后进行Sorting排序 
) A WHERE Member.ID=A.ID --直接取值,不排序

  效果图:

 

posted @ 2018-03-09 15:49  AnAng  阅读(841)  评论(0编辑  收藏  举报