好的,这是我同时提出的另一种解决方案。我决定用户已经以随机顺序在系统中,所以也许我应该尝试解码器环样式的方法。因此,此代码生成一个移位值,然后移动用户 ID。select 语句使用公式进行“环绕”。它不是完全随机的,它可以使用一些润色,但这就是我所拥有的。用户可能不会意识到缺乏随机性。谢谢您的帮助!
DECLARE @people TABLE
(
id INT,
name VARCHAR(50)
)
INSERT INTO @people VALUES (1,'Matthew')
INSERT INTO @people VALUES (2,'Mark')
INSERT INTO @people VALUES (3,'Luke')
INSERT INTO @people VALUES (4,'John')
INSERT INTO @people VALUES (5,'Doug')
INSERT INTO @people VALUES (6,'Jamie')
INSERT INTO @people VALUES (7,'John')
INSERT INTO @people VALUES (8,'Cameron')
INSERT INTO @people VALUES (9,'Emily')
INSERT INTO @people VALUES (10,'Tyler')
INSERT INTO @people VALUES (11,'Todd')
INSERT INTO @people VALUES (12,'Kathryn')
DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT;
DECLARE @MaxId INT;
SET @Lower = 1 -- The lowest record ID
SELECT @Upper = (MAX(Id) / 2) FROM @people -- select random int that is somewhere between 1 and half of max ID for shift
SELECT @MaxId = MAX(Id) FROM @people -- select the largest ID in the table
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) -- calculate a random number to shift IDs off by
SELECT id, ((id + @Random) % @MaxId) + 1, @Random
from @people