是的,您可以通过首先获取中间值来在MySQL中执行此操作。让我们首先创建一个表:
create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY );
以下是使用insert命令在表中插入一些记录的查询:
insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values(); insert into DemoTable values();
以下是使用select命令显示表中记录的查询:
select *from DemoTable;
这将产生以下输出
+--------+ | UserId | +--------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | +--------+ 10 rows in set (0.00 sec)
这是首先获取中间值的查询:
set @middleValue=(select max(UserId) from DemoTable)/2;
现在,让我们交替获取升序和降序值:
select *from DemoTable ORDER BY (IF(UserId <@middleValue,@middleValue*2- UserId,UserId-1)) DESC,UserId ASC;
这将产生以下输出
+--------+ | UserId | +--------+ | 1 | | 10| | 2 | | 9 | | 3 | | 8 | | 4 | | 7 | | 6 | | 5 | +--------+ 10 rows in set (0.00 sec)