简体   繁体   English

MySQL如何使用随机数创建动态表

[英]MySQL How to create a dynamic table with Random number

here is my example: 这是我的例子:

This is a table which i will create, (static) 这是我将创建的表,(静态)

Id   Image
1     a.jpg
2     b.jpg
3     c.jpg

Now taking the Id as foreign key I want to create a dynamic table as 现在以Id作为外键,我想创建一个动态表

Rid Id
1    2
2    1
3    1
4    3
5    2 
6    3
7    3
8    2
9    2
10   1

Note that Id is random. 请注意,Id是随机的。 How can I? 我怎样才能?

/*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           */

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

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