Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
random number inserted into database rows
Old 02-01-2010, 11:11 PM random number inserted into database rows
Novice Talker

Posts: 6
Name: john
Trades: 0
Hello,

I was wondering if anyone know a way where I can change all of the rows for the column views in a certain table to a random number?

such as they all change to something over 5000 but nothing more than 10000?
potato is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-02-2010, 07:26 AM Re: random number inserted into database rows
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Depends of the db used.
If mysql, then this is how:
Code:
create table if not exists random (
    id int auto_increment,
    nbr int,
    primary key (id)
);
truncate table random;

create temporary table if not exists cr1 (
    nbr int
);
truncate table cr1;
create temporary table if not exists cr2 (
    nbr int
);
truncate table cr2;
create temporary table if not exists cr3 (
    nbr int
);
truncate table cr3;

/*We use those to generate entries*/
insert into cr1 (nbr) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(0);
insert into cr2 (nbr) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(0);
insert into cr3 (nbr) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(0);

/*Create those entries in the table*/
insert into random (nbr)
select 1
from cr1
    cross join cr2
    cross join cr3
;

/*Assign a random number between 5000 and 10000*/
update random
set nbr=FLOOR(5000 + (RAND() * 10000));

/*And voila...*/
SELECT *
from random;
Of course, skip to the update if you don't need to generate the datas.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 02-02-2010 at 07:28 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to random number inserted into database rows
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17893 seconds with 12 queries