The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

MYSQL Update Random Row? Possible? by MikeCDN2005-05-04 12:53:17
  Here's what I would do in T-SQL. by YakkoWarner 2005-05-04 14:05:13

Hopefully someone could translate into MySQL...

Declare @ID int Set @ID = null
While Not Exists (Select * From info_grid Where ID = @ID)
Select @ID = floor( rand() * (max(id)-min(id)) ) + min(id) From info_grid

Update info_grid Set setting = @data Where ID = @ID

In other words, grab a random integer between the smallest and largest ID. If that ID doesn't exist, grab another one until you find one that does. Then update. This should work fairly quickly if there aren't major holes in your IDs (otherwise it may take the loop longer to find a random number that does exist).

This of course assumes your ID field is an integer. If you're using GUIDs, it won't help you...

[ Reply ]
    Best case: O(1). Worst case: infinity! (n/t) by Arachnid2005-05-04 14:17:40

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)