简体   繁体   中英

Group mysql rows in an array by column value

I have a mysql table in this format

Mysql table format
_______________________________
| column1 | column2 | column3 |
|_________|_________|_________|
|   A     |  val Z  |  val Y  |
|_________|_________|_________|
|   A     |  val X  |  val W  |
|_________|_________|_________|
|   A     |  val V  |  val U  |
|_________|_________|_________|
|   B     |  val T  |  val S  |
|_________|_________|_________|
|   B     |  val R  |  val Q  |
|_________|_________|_________|
|   C     |  val P  |  val O  |
|_________|_________|_________|

What I need to do is return all rows of an occurrence of each value in column1 in an array. The array may be something like this:

$A = array($row1,$row2,$row3);

$row1 = array("column1"=>"A", "column2"=>"val Z", "column3"=>"val Y",);

$row2 = array("column1"=>"A", "column2"=>"val X", "column3"=>"val W",);

$row3 = array("column1"=>"A", "column2"=>"val V", "column3"=>"val V",);

I hope I am clear enough. Basically I need rows to be grouped by uniqueness of values in column1.

The mysql result should contain all the rows in an array grouped as said above. something like this:

$result = array($A, $B, $C);

What I need to do with the output is display it in a table. If there is a better way of doing it please let me know. I would like the table to take this format:

_________________________________________________
|  Header1  |  Header2  |  Header3  |  Header4  |
|___________|___________|___________|___________|
|    A      |   val Y   |   val W   |   val U   |
|___________|___________|___________|___________|
|    B      |   val S   |   val Q   |           |
|___________|___________|___________|___________|
|    C      |   val O   |           |           |
|___________|___________|___________|___________|

This is the only way I can only think of doing it. If there is a better way of doing it please let me know. Thank you for helping.

Assuming the fact that you want to display only the column3 data in the table, the solution would be like this:

Change your query from

SELECT * FROM table;

to

SELECT column1, GROUP_CONCAT(column3 SEPARATOR ',') as column3 
FROM table 
GROUP BY column1 
ORDER BY COUNT(column1) DESC

Execute the query and follow the below steps,

  1. Construct an associative array from the result set.
  2. Extract the keys from the associative array.
  3. Count the number of columns in the table, and finally
  4. Construct the table

Here's the code:

$sql = "SELECT column1, GROUP_CONCAT(column3 SEPARATOR ',') as column3 FROM table_name GROUP BY column1 ORDER BY COUNT(column1) DESC";
$result_set = mysqli_query($conn, $sql);

// construct an associative array
$result_array = array();
while($row = mysqli_fetch_assoc($result_set)){
    $result_array[$row['column1']] = explode(",", $row['column3']);
}

// extract the keys from the associative array
$result = array_keys($result_array);

// count the number of columns in the table
$len = count($result_array[$result[0]]);

// construct the table
echo '<table border="1">';

// display table headings 
echo '<tr>';
for($i = 1; $i <= $len + 1; ++$i){
    echo '<th>Header' . $i . '</th>';
}
echo '</tr>';

// display table contents
foreach($result as $k){
    echo '<tr>';
    echo '<td>' . $k . '</td>';
    for($i = 0; $i < $len; ++$i){
        echo '<td>';
        echo isset($result_array[$k][$i]) ? $result_array[$k][$i] : '';
        echo '</td>';
    }
    echo '</tr>';
}

echo '</table>';

Output:

在此处输入图片说明


Note: If you want to see the structure of $result_array array, do var_dump($result_array);

I will not write all code, but give you a sketch of solution:

1) feth all give you array with with rows. Call it $rows .

2) do foreach ($rows as $row) . Inside create temporary array with values from column 1 . If value from column 1 in given $row do not exists in temporary array, add it to temporary array ad do output[] = row . Else if temporary array contain value from column 1 on n-th position, do $output[n][] = $row

Output should have desired form after this loop. More intuitive is keep values of column 1 as keys in $output and remove it from rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM