MySQL如何创建随机数的动态表

MySQL如何创建随机数的动态表,mysql,random,Mysql,Random,以下是我的例子: 这是我将创建的一个表,静态 Id Image 1 a.jpg 2 b.jpg 3 c.jpg 现在将Id作为外键,我想创建一个动态表作为 Rid Id 1 2 2 1 3 1 4 3 5 2 6 3 7 3 8 2 9 2 10 1 请注意,Id是随机的。 我该怎么办 /*example table*/ create table foo(id int auto_increment pri

以下是我的例子:

这是我将创建的一个表,静态

Id   Image
1     a.jpg
2     b.jpg
3     c.jpg
现在将Id作为外键,我想创建一个动态表作为

Rid Id
1    2
2    1
3    1
4    3
5    2 
6    3
7    3
8    2
9    2
10   1
请注意,Id是随机的。 我该怎么办

/*example table*/
create table foo(id int auto_increment primary key, rnd int);
/*now insert as much as necessary*/
/*this will create one entry*/
insert into foo(rnd)
select rand() * 2 + 1;
/*you can also use a table which has more rows than you need*/
insert into foo(rnd)
select rand() * 2 + 1 from whatever_table limit 10; /*limit it to 10 entries*/
                                                    /*or whatever           */