在找不到任何符合我需要的东西后,我编写了这段代码来一致地打乱 mysql 中列的值。有一个更好的方法吗?
**Original table:**
+----+-----------+
| id | fname |
+----+-----------+
| 1 | mike |
| 2 | ricky |
| 3 | jane |
| 4 | august |
| 6 | dave |
| 9 | Jérôme |
+----+-----------+
**Possible output:**
+----+-----------+
| id | fname |
+----+-----------+
| 1 | dave |
| 2 | jane |
| 3 | mike |
| 4 | ricky |
| 6 | Jérôme |
| 9 | august |
+----+-----------+
DROP TEMPORARY TABLE IF EXISTS shuffle1;
DROP TEMPORARY TABLE IF EXISTS shuffle2;
CREATE TEMPORARY TABLE shuffle1 (id int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), original_values varchar(255), key original_values(original_values) );
CREATE TEMPORARY TABLE shuffle2 (id int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), original_ids int(11), key original_ids(original_ids) );
INSERT INTO shuffle1 (id, original_values) SELECT NULL, table1.fname FROM table1 ORDER BY rand();
INSERT INTO shuffle2 (id, original_ids) SELECT NULL, table1.id FROM table1;
UPDATE table1 SET table1.fname = (SELECT shuffle1.original_values FROM shuffle1 JOIN shuffle2 ON shuffle2.id = shuffle1.id WHERE table1.id = shuffle2.original_ids);