如何在MySql中随机播放列 [英] How to shuffle a column in MySql

查看:48
本文介绍了如何在MySql中随机播放列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在找不到满足我需求的任何东西后,我编写了这段代码,以一致地对mysql中的列值进行改组.有更好的方法吗?

After failing to find anything that matched my needs, I wrote this code to consistently shuffle the values of a column in mysql. Is there a better way to do this?

**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);

推荐答案

如果您不介意某些表数据无法处理(取决于表大小,预计50%的行不能进行处理),这是一个解决方案:

If you don't mind around some of the table data cannot be processed (depends on table size, expect 50% row cannot be processed), here's a solution:

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.

Note: You might find the query very slow, this is because it join the table twice, so if data size is 1000, it will need to process 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

You can control the speed of the query by chaging ON new_meme.id <> original_meme.id to ON new_meme.id BETWEEN original_meme.id - 5 AND original_meme.id +5 (the 5 can be change), but it will decrease randomness and not worked for non-numberical id

这篇关于如何在MySql中随机播放列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆