Selecting data with multiple priorities and limited max entries

#1

I need to create a PHP script that selects data from MySQL table with multiple priorities?

Every MySQL row is an application for several “sports discipline” with sportsman score as the key for rating.

Sports columns holds the priority for each participient wish to participiate in exact sport. There are limited places for each sports.

That means If his first priority doesn’t met (eg. he is 31st in the list, but there are only 30 places available) then the next priority must be as his wish.

An example:

[code]ID | Name | Tenis | Swiming | Basketball | Runing | Score|

1 |John |0 |1 |2 |3 |80 |

1 |Bill |1 |2 |3 |4 |65 |

1 |Arnold |0 |1 |0 |2 |70 |
-----------------------------------------------------------------------[/code]

Every disciple has its maximum participients. For an example:

[php]
$max_tenis = 5;

$max_swiming = 8;

$max_basketball = 10;

$max_runing = 9;
[/php]

I qould like to select all applications for each sports and checking if they are met with the limitation set.

So that if I’m selecting all basketballs I must select all other applications with priorities more than the first priority (or second, third…) if the last priority do not met limitation.