我有两个表,我想用随机值的表的 ID更新GebruikerID表中的列。TopicGebruiker
你还必须知道以下几点:
Topic.GebruikerID引用Gebruiker.ID.Gebruiker(列ID)中,必须更新到表Topic(列GebruikerID)中。这里有一些图片。
您可以ROW_NUMBER根据随机排序分配两个s 并加入它们:
select
t.id,
g.id as gebruikerid
from
(
select id,
(row_number() over (order by newid())
% (select count(*) from gebruiker))+1 as rn -- number from 1 to n
from topic
) as t
join
( select id,
row_number() over (order by newid()) as rn -- sequential number from 1 to n
from gebruiker
) as g
on t.rn = g.rn
order by t.id
Run Code Online (Sandbox Code Playgroud)
见小提琴
编辑:
您可以使用此 SELECT 的结果来更新Topic表:
update t
set gebruikerid = dt.gebruikerid
from topic as t
join
(
select
t.id,
g.id as gebruikerid
from
(
select id,
(row_number() over (order by newid()) % (select count(*) from gebruiker))+1 as rn
from topic
) as t
join
( select id,
row_number() over (order by newid()) as rn
from gebruiker
) as g
on t.rn = g.rn
) as dt
on t.id = dt.id;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6155 次 |
| 最近记录: |