Alternate GROUP BY

Hi,

Is it possible to alternate GROUP BY?

For e.g
ive got a table(name, votes_up, votes_down)

first result = highest vote
second result = lowest vote
third result = highest vote
fourth result = lowest vote

presumably you are using GROUP BY in order to count the votes?

you will have to give a more detailed example of what you want

why would you repeat the highest and lowest in the third and fourth results?

Hi. Sorry I meant the next highest result.

1st result - highest vote
2nd result - lowest vote
3rd result - next highest vote
4th result - next lowest vote

and so on…

yes, i guess you could accomplish something like that with sql, but it would be very complicated indeed

why can’t you do it in php? return the results from a simple query, load them into an array, and start printing the array from both ends

the tricky part, whether you do it in sql or in php, is knowing where the middle is, and when to stop printing as the high scores overlap the low scores, and i suspect this will be a lot easier in php


<?php

// grab the votes ordered by the number of votes

// Sample result below
$votes = array    (
                    1 => array('name' => 'John', 'vote' => 55),
                    2 => array('name' => 'Bill', 'vote' => 45),
                    3 => array('name' => 'Brian', 'vote' => 35),
                    4 => array('name' => 'David', 'vote' => 25),
                );

$count=1;
$vote_result=array();

while (!empty($votes)) {
    
    if ( $odd = $count%2) {
        $vote_name=array_shift($votes);
        array_push($vote_result,$vote_name);
    } else {
        $vote_name=array_pop($votes);
        array_push($vote_result,$vote_name);
    }
    $count++;
}

echo '<pre>';
var_dump($vote_result);
echo '</pre>';

?>

That assumes that you’ve taken the number of negative votes from the number of positive votes for each row.