Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>SELECT company, GROUP_CONCAT(value SEPARATOR ',') AS value, GROUP_CONCAT(monthyear SEPARATOR ',') AS monthyear FROM yourTable GROUP BY company </code></pre> <p>Some Reference for <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" rel="nofollow"><code>GROUP_CONCAT</code></a>.</p> <p>PHP solution:</p> <p>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 <code>value</code> and <code>monthyear</code> to the current company. </p> <p>You could do this even without sorting:</p> <pre><code>while($row = mysql_fetch_assoc($resource)) { $values[$row["country"]][] = $row["value"]; $monthyear[$row["country"]][] = $row["monthyear"]; } </code></pre> <p>Some output example</p> <pre><code>foreach ($values as $country =&gt; $valuesOneCountry) { // each country var_dump($country); foreach ($valuesOneCountry as $i =&gt; $value) { // value, monthyear for each original row var_dump($value, $monthyear[$country][$i]); } } </code></pre> <p>Elegant way with OOP:</p> <pre><code>class Tuple { public $country, $values, $monthyears; public function __construct($country, $values = array(), $monthyears = array()) { $this-&gt;country = $country; $this-&gt;values = $value; $this-&gt;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-&gt;addValue($row["value"]); $tuple-&gt;values[] = $row["value"]; $tuple-&gt;monthyears[] = $row["monthyear"]; } var_dump($tuples); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload