0

I broke my head about this, but couldn't find a solution. Let's take the following example:

There is a game and a user can find stuff. But you can't find all the stuff equally. So there is a table with the items, and thereby a number (let's say between 0 and 5) that will say how much the chance is that this will be found.

So if you have 3 items from 5, 2 and 3:

  • First item: 50% chance ( 5/(5+2+3) )
  • Second item: 20% chance ( 2/(5+2+3) )
  • Third item: 30% chance ( 3/(5+2+3) )

Is this possible with just SQL or do you really need another language like PHP too?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Olivier Van Bulck
  • 751
  • 1
  • 8
  • 22
  • possible duplicate of [MySQL: Select Random Entry, but Weight Towards Certain Entries](http://stackoverflow.com/questions/2417621/mysql-select-random-entry-but-weight-towards-certain-entries) – Raphaël Althaus Jan 12 '14 at 12:08

1 Answers1

0

Try this

SELECT item , (itemNumber / (Select SUM(itemNumber) from itemTable) )*100
FROM itemTable
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • Yes, that calculates the percentages and I know how to do that, but I want to select a random row from the table, dependent on the percentage of chance to be selected actually. – Olivier Van Bulck Jan 12 '14 at 13:32