【发布时间】:2019-05-28 09:58:50
【问题描述】:
关于我之前尚未回答的问题How to group array value duplicate and customize the display of the results of the array in html table?,我选择以其他方式仅显示 5 行。
这是数据库:
lid class_id class Q1-2 Q3-4 total item_group
----- ------- ----- ----- ----- ----- ----------
1 73 Leader 5000 50000 10000 33
1 77 Consultant 4000 4000 8000 33
1 83 Coordinator 3000 3000 6000 33
2 73 Leader 10000 10000 20000 33
2 76 Staff 4000 4000 8000 33
2 77 Consultant 5000 50000 10000 33
3 73 Leader 15000 15000 30000 33
3 78 Team Leader 4000 4000 8000 33
这是我使用的 SQL 查询:
$sql = "SELECT *, group_concat(AZ.lid) as lids, group_concat(AZ.total) AS totals FROM (
SELECT * FROM view_items WHERE lid='1'
UNION
SELECT * FROM view_items WHERE lid='2'
UNION
SELECT * FROM view_items WHERE lid='3'
) AS AZ WHERE item_group='33' GROUP BY class_id";
这是 SQL 结果:
class id class lid Q1-2 Q3-4 lids totals item_group
--------- ----- ------ ----- ----- ----- ------ ----------
73 Leader 1 5000 5000 1,2,3 10000,20000,30000 33
77 Consultant 1 4000 4000 1,2 8000,10000 33
83 Coordinator 1 3000 3000 1 6000 33
76 Staff 2 2 8000 33
78 Team Leader 3 3 8000 33
$proj_lid->lid is equals to 1 (Displayed in the P1 column only)
$row->lid is equals to 1 (Displayed in the P1 column only)
$limit is 3 (The count of P's)
这是html表格部分:
<?php
for($iy=1; $iy<=$limit; $iy++){
$sql_lid = "SELECT * FROM view_items WHERE lid='$iy' GROUP BY lib_id ORDER BY lid ASC";
$query_lid = $this->db->prepare($sql_lid);
$query_lid->execute();
$res=$query_lid->fetch();
$lids = $res->lid;
$lid_arr[] = $lids ;
$sql_arr = "";
if($iy==1) $sql_arr .= "SELECT *, group_concat(AZ.lid) as lids, group_concat(AZ.total) AS totals FROM (";
$sql_arr.="SELECT * FROM view_items WHERE lid='$lids'";
if($iy!=$limit) $sql_arr .= " UNION ";
if($iy==$limit) $sql_arr.=" ) AS AZ WHERE item_group='33' GROUP BY class_id";
$sqlArr[] = $sql_arr;
}
$sql = implode("",$sqlArr);
$query = $this->db->prepare($sql);
$query = $this->db->prepare($sql);
$query->execute();
$result = $query->fetchAll();
foreach($result as $row){
if($row->item_group == "33"){
$q1 = ($row->q1-2)
$q3 = ($row->q3-4)
$total = $q1 + $q3;
?>
<tr>
<td><?php echo $row->class; ?></td>
<td><?php if($q1 != 0 && $proj_li->lid == $row->lid){ echo ($q1 < 0 ? "(".number_format(abs($q1),2).")" : number_format($q1,2));} else{ echo "-";} ?> </td>
<td><?php if($q3 != 0 && $proj_lid->lid == $row->lid ){ echo ($q3 < 0 ? "(".number_format(abs($q3),2).")" : number_format($q3,2));} else{ echo "-";} ?> </td>
<td><?php if($total != 0 && $proj_lid->lid == $row->lid ){ echo ($total < 0 ? "(".number_format(abs($total),2).")" : number_format($total,2));} else{ echo "-";} ?> </td>
<?php
$totals = explode(", ", $row->totals);
foreach ($totals as $rowstotals) {
}
for($i = 1; $i <= $limit; $i++){
if ($i != $proj_lid->lid && $proJ-lid->lid < $i){
?>
<td><?php if($rowstotals != 0){ echo ($rowstotals < 0 ? "(".number_format(abs($rowstotals),2).")" : number_format($rowstotals,2));} else{ echo "-";}?></td>
<?php
} else{ }
}
}
}
?>
</tr>
我设法显示了预期的输出
预期的输出(只有 5 行,没有重复的类 id)
但是在单独显示 group_concat 值时出现了另一个问题。我需要 P2 和 P3 中的值来仅显示来自 group_concat 的一个结果。
这应该是预期的输出
更新:我发现(可能)与我想做的类似的输出(忘记了我在哪里看到这个链接) Link
【问题讨论】:
-
我在问题中说我选择了其他方式(关于我之前的问题)。这是一个不同的问题/问题。感谢您帮助新手
-
如果你展示你的数据库设计可能会更清楚。现在我们只能猜测所有这些数字的来源。
-
添加你的html代码
-
按照你们的建议添加。谢谢你。 @Omi
-
我不了解 html,但我知道从 MySQL 查询中执行此操作将非常具有挑战性。
标签: php mysql group-concat