Jump to content

Random row with extras


stvn

Recommended Posts

Hello everybody,

I need a little help with extracting a random row from a DB table.

I have a forum and I want to make weekly draws for people who posted that specific week so they can win stuff.

My current code is simple:

SELECT * FROM forum_members WHERE number_of_posts>0 ORDER by RAND() LIMIT 1

 

but I want to change that code somehow because this way users have equal chance regardless of how many posts they have, it would be more fair if users with more posts to have more chance of winning.

 

Any ideas?

 

Thank you ans sorry for my english, I hope you understud what I ment.

 

Link to comment
Share on other sites

Thank you for your reply fenway, but unfortunately this is not what I'm lookong for, I guess I just can't explain,let me try again:

 

So, my table looks like this:

 

idnameposts

1user19

2user25

3user33

4user41

5user50

 

Now if I'd make the draw using a hat and paper clippings I'd write user1 on 9 pieces of paper, user2 on 5 pieces, user3 on 5 pieces, etc.. so user1 has the biggest chance of winning.

 

The question is that can this be done in some simple way and can you or anyone give me some info on where and how should i start, and as you probably noticed I'm no guru at this , just trying to lear.

Thank you.

Link to comment
Share on other sites

Sorry, I misread your original post, I thought you wanted them all weighted equally.

 

The problem is that SQL doesn't really have a mechanism for "extrapolating" rows in a way that would make this easy.

 

Depending on how "random" you want it to be, you can multiply the number of posts by RAND(), and pick the highest value...

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.