Question

I have this search running successfully that returns players skills and attributes. I want to get the top ten percent in particular columns. I was trying to use this:

$j=0;
while($percentile_row = mysql_fetch_array($percentile_result)){
    echo $percentile_row['user_id'].'<br />';
    $j++;
}
echo 'Total: '.$j.'<br />';
echo round(100/$j,1);

$players_worth = round(100/$j,1);

while($players_worth > 10){
    echo 'adding<br/>';
    $players_worth + $players_worth;
}

It seemed like a simple way to get it. It's just that the $players_worth + $players_worth; part isn't working when it's within the while loop. It works if I just echo it.

Or is there something I can add to the query that will make it select by a percentage? I have to do this for about 15 columns.

This is my existing query:

SELECT user_id FROM Players ORDER BY user_id DESC

Thank you for any tips you might have.

Edit:
I tried this, but no luck, when I var_dump it, it just says "bool(false)": MySQL: LIMIT by a percentage of the amount of records?

Was it helpful?

Solution 2

I got it and here's how I went about it:

First I got all the rows, then...

echo 'Total rows in table: '.$j.'<br />';

$twentyfive = floor($j*.25);
$fifteen = floor($j*.15);
$ten = floor($j*.1);


echo $ten.' '.$fifteen.' '.$twentyfive.'<br /><br />';


$topten=0;

while($topten < $ten && $percentile_row2 = mysql_fetch_array($percentile_result2)){

    $change =   "UPDATE Players SET " . $column['field'] ."=concat(" . $column['field'] . ",'%') WHERE user_id='" . $percentile_row2['user_id'] . "'";

    $wpdb->get_results($change);

    echo '<strong>in top 10%</strong> - '.$percentile_row2['field_1'].' - '.$percentile_row2['user_id'].'<br />';

$topten++;

}

And so on for the other percentiles. All the echoes were just to track it.

OTHER TIPS

If you already know the number of records in the database, you could use LIMIT:

"SELECT user_id FROM Players ORDER BY user_id DESC LIMIT ".$rowCount*$percentage/100

I don't know of any way to use a percentage directly in a query, unless you already know the cutoff point for the value you're checking, which means you've already gone through the database and examined all the values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top