如何在MySql中混洗列

时间:2013-08-29 18:35:48

标签: mysql shuffle

在找不到符合我需求的任何内容后,我编写了这段代码,以便在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);

2 个答案:

答案 0 :(得分:0)

如果您不介意某些表数据无法处理(取决于表大小,预计无法处理50%的行),请采用以下解决方案:

原始表

id  name
1   Some
2   Body
3   Once
4   Told
5   Me
6   The
7   World
8   Is
9   Gonna
10  Roll
11  Me
12  I 
13  Ain't
14  The 
15  Shapest
16  Tool
17  In
18  The
19  Shed
20  She
21  was
22  looking
23  kind
24  of
25  dumb
26  with
27  her
28  finger
29  and
30  her
31  thumb

查询:

SELECT new_id, name FROM (
    SELECT  new_id, name FROM (
        SELECT new_meme.id as new_id, original_meme.id as original_id, original_meme.name FROM meme original_meme
        JOIN meme new_meme ON new_meme.id <>  original_meme.id
        ORDER BY RAND()
    ) layer1
    GROUP BY layer1.new_id
) layer2 GROUP BY name

结果(当然,每次运行都不同)

1   I 
2   In
3   Gonna
4   Ain't
5   The
6   her
7   finger
8   Some
9   dumb
10  She
15  Me
16  with
17  Told
18  and
19  World
21  Roll
22  The 
25  Tool
26  Shed
27  Is
28  Me
29  Sharpest
31  The

注意:您可能会发现查询非常慢,这是因为它两次连接了表,因此如果数据大小为1000,则需要处理1000 * 1000。

您可以通过将ON new_meme.id <> original_meme.id切换为ON new_meme.id BETWEEN original_meme.id - 5 AND original_meme.id +5(可以更改5)来控制查询的速度,但是它将降低随机性,不适用于非数字ID

答案 1 :(得分:-1)

UPDATE table1 SET fname=(SELECT t.fname FROM(SELECT fname FROM table1) t ORDER BY RAND() LIMIT 1)