Jump to content

mySQL random


squiggerz

Recommended Posts

I have a script that pulls code from a field in a row in mySQL db randomly, is there a way to have several instances of this pulling in the same script, pulling randomly from the remaining rows without repeating? I guess what I'm needing is a function to lock the row once it has been used in the script so that it doesnt use the same row again, then unlock the rows once the script is complete. Is this even possible?

Link to comment
Share on other sites

Create a field in the database called 'lock' with an ENUM('0','1'), select only rows where `lock`='0' and set `lock`='1' on selected rows, then at the end of the script just set `lock`='0' again.

 

Or, select all your rows at once, in a random order with "ORDER BY RAND() LIMIT 5", store them in an array, and loop through the array as and when you need one of the rows.

Link to comment
Share on other sites

Maybe just have the query keep repeating and if $result2 = $result1, then repeat query until otherwise.

 

or

 

after getting $result1

 

then $result2 = mysql_query(SELECT * FROM table WHERE identification NOT CONTAINS $result1)

 

modify that how you may, but currently i think that will pull everything except $result1

 

NOT CONTAINS $result1 OR $result2

... not sure how many times you can use OR over again...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.