简体   繁体   English

选择所有MySQL行并随机重新插入

[英]Select all MySQL rows and re insert randomly

So I have a table in my database which has some 1000 rows in it. 所以我的数据库中有一个表,其中有大约1000行。

Now I don't want to display these records in the same order they are inserted into the table. 现在,我不想以将它们插入表中的顺序显示这些记录。

I want all the rows in the table to be somehow randomly shuffled. 我希望表中的所有行都以某种方式随机洗牌。 Ex : if I have 10 records in the order 1 2 3 4 5 6 7 8 9 10 , I want it like 8 2 4 6 3 1 7 9 10 5 . 例如:如果我有10条记录以1 2 3 4 5 6 7 8 9 10的顺序排列,我希望它像8 2 4 6 3 1 7 9 10 5

The rand() method can be used but it's not efficient for large data. 可以使用rand()方法,但对大数据而言效率不高。 I can fetch the data and shuffle in PHP but I don't want to use it because then I would have to shuffle it every time I fetch the data. 我可以获取数据并在PHP中进行随机播放,但是我不想使用它,因为那样的话,每次获取数据时都必须对其进行随机播放。

So basically I don't want to shuffle and fetch the data but shuffle all the rows in table itself. 因此,基本上我不想改组和获取数据,而是改组表本身中的所有行。

Just once shuffling my rows in table is more than enough for me. 一次对表中的行进行改组对我来说绰绰有余。

You create a second table, duplicating the original structure, then dump all the records into it. 您创建另一个表,复制原始结构,然后将所有记录转储到该表中。 Rename the tables and you're done. 重命名表即可。 It'd be worth rebuilding any indexes on the new table and you might hit a few snags with mySql getting fussy about re-using old names. 重建新表上的任何索引都是值得的,并且您可能会因mySql遇到一些麻烦而对重用旧名称感到困惑。

INSERT INTO <copy of source table>
SELECT *
FROM <source table>
ORDER BY RAND()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM