按列值对数组中的 mysql 行进行分组

问题描述 投票:0回答:2

我有一个这种格式的mysql表

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  |
|_________|_________|_________|

我需要做的是返回数组中第 1 列中每个值出现的所有行。数组可能是这样的:

$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",);

我希望我说得足够清楚。基本上我需要根据第 1 列中值的唯一性对行进行分组。

mysql 结果应包含如上所述分组的数组中的所有行。像这样的:

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

我需要对输出执行的操作是将其显示在表格中。如果有更好的方法,请告诉我。我希望表格采用这种格式:

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

这是我唯一能想到的办法。如果有更好的方法,请告诉我。谢谢你的帮助。

php mysql grouping
2个回答
5
投票

假设你想要只显示表中的column3数据,解决方案将是这样的:

更改您的查询

SELECT * FROM table;

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

执行查询并按照以下步骤操作,

  1. 从结果集中构造一个关联数组。
  2. 从关联数组中提取键。
  3. 统计表格的列数,最后
  4. 构建表格

这是代码:

$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>';

输出:


注意: 如果你想查看

$result_array
数组的结构,请执行
var_dump($result_array);


0
投票

我不会写所有代码,但给你一个解决方案的草图:

1)

feth all
为您提供带有行的数组。就叫它
$rows

2)做

foreach ($rows as $row)
。里面 使用
column 1
中的值创建临时数组。如果临时数组中不存在给定
$row
中第 1 列的值,请将其添加到临时数组并执行
output[] = row
。否则,如果临时数组包含第 n 个位置上的
column 1
的值,则执行
$output[n][] = $row

此循环后输出应具有所需的形式。更直观的是,将

column 1
的值保留为
$output
中的键,并将其从行中删除。

© www.soinside.com 2019 - 2024. All rights reserved.