score:2

Accepted answer
select
    company, 
    group_concat(value separator ',') as value, 
    group_concat(monthyear separator ',') as monthyear
from
    yourtable
group by
    company

some reference for group_concat.

php solution:

select the to be grouped attribute sorted (company). loop over them and open a new group every time you encounter a different value for company. as long as the current row has the same row as the previous, add value and monthyear to the current company.

you could do this even without sorting:

while($row = mysql_fetch_assoc($resource))
{
    $values[$row["country"]][] = $row["value"];
    $monthyear[$row["country"]][] = $row["monthyear"];
}

some output example

foreach ($values as $country => $valuesonecountry)
{
    // each country
    var_dump($country);

    foreach ($valuesonecountry as $i => $value)
    {
        // value, monthyear for each original row

        var_dump($value, $monthyear[$country][$i]);
    }
}

elegant way with oop:

class tuple
{
    public $country, $values, $monthyears;

    public function __construct($country, $values = array(), $monthyears = array())
    {
        $this->country = $country;
        $this->values = $value;
        $this->monthyears = $monthyears;
    }
}

$tuples = array();
while($row = mysql_fetch_assoc($resource))
{
    if (!isset($tuples[$row["country"]]))
         $tuples[$row["country"]] = new tuple($row["country"]);

    // save reference for easy access        
    $tuple = $tuples[$row["country"]];

    // or some method like $tuple->addvalue($row["value"]);
    $tuple->values[] = $row["value"];
    $tuple->monthyears[] = $row["monthyear"];
}

var_dump($tuples);

Related Query

More Query from same tag