[英]How I can mix column data
我有一張桌子table_1
id contact_id
1 500
5 89
8 35
15 458
... ...
555 38
如何混合contact_id
和table_1
結果
id contact_id
1 35
5 458
8 35
15 89
... ...
555 45
您可以使用變量或row_number()
(在MySQL 8+中)隨機分配聯系人ID:
select t1.id, tt1.contact_id
from (select t1.*, row_number() over (order by id) as seqnum
from table_1 t1
) t1 join
(select t1.*, row_number() over (order by rand()) as seqnum
from table_1 t1
) tt1
on t1.seqnum = tt1.seqnum;
沒有太多困難(但需要更多鍵入),可以將其轉換為使用早期版本中的變量。
如果要永久地改組值,也可以將其合並到update
語句中。
編輯:
我想你要:
update table1 t1 join
(select t1.id, tt1.contact_id
from (select t1.*, (@rn1 := @rn1 + 1) as seqnum
from (select * table_1 order by id) t1 cross join
(select @rn1 := 0) params
) t1 join
(select t1.*, (@rn2 := @rn2 + 1) as seqnum
from (select * from table_1 order by rand()) t1 cross join
(select @rn2 := 0) params
) tt1
on t1.seqnum = tt1.seqnum
) tt1
on tt1.id = t1.id
set t1.contact_id = tt1.contact_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.