我需要显示按某个数字列排序的数据库表中的记录列表。该表如下所示:
CREATE TABLE items (
position int NOT NULL,
name varchar(100) NOT NULL,
);
INSERT INTO items (position, name) VALUE
(1, 'first'),
(5, 'second'),
(8, 'third'),
(9, 'fourth'),
(15, 'fifth'),
(20, 'sixth');
现在,列表的顺序应该根据用户提供的参数而改变。此参数指定首先出现的记录,如下所示:
position = 0
order should be = 1, 5, 8, 9, 15, 20
position = 1
order should be = 20, 1, 5, 8, 9, 15
position = 2
order should be = 15, 20, 1, 5, 8, 9
换句话说,最后一条记录成为第一条,依此类推。你能想出一种在 SQL 中做到这一点的方法吗?
我正在使用 MySQL,但任何 SQL 数据库中的示例都可以。
谢谢