如何在html table 单元格中分别显示group_concat 结果?
How to display group_concat result seperately in html table cell?
关于我之前的问题 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 table 部分:
<?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 行,没有重复的 class id)
但是在单独显示 group_concat 值时出现了另一个问题。我需要 P2
和 P3
中的值来仅显示来自 group_concat.
的一个结果
这应该是预期的输出
更新: 我发现(也许)与我想做的类似的输出(忘了我在哪里看到的 link)
Link
这就是我的想法:
SELECT lid, class_id,class, `Q1-2`,`Q3-4`,total,
CASE WHEN COUNT(*)>=2 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(total ORDER BY lid ASC SEPARATOR ' '),' ',2),' ',-1) END AS P2,
CASE WHEN COUNT(*)>=3 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(total ORDER BY lid ASC SEPARATOR ' '),' ',3),' ',-1) END AS P3
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 ORDER BY lid ASC;
我在一个等于或大于 2 的 GROUP_CONCAT
值上使用了两次 SUBSTRING_INDEX
函数,但在这个例子中我在计数为 3 时停止。我还在 GROUP_CONCAT
中添加了 ORDER BY lid ASC
以确保它将 return 按 lid
.
排序的值
关于我之前的问题 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 table 部分:
<?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 行,没有重复的 class id)
但是在单独显示 group_concat 值时出现了另一个问题。我需要 P2
和 P3
中的值来仅显示来自 group_concat.
这应该是预期的输出
更新: 我发现(也许)与我想做的类似的输出(忘了我在哪里看到的 link) Link
这就是我的想法:
SELECT lid, class_id,class, `Q1-2`,`Q3-4`,total,
CASE WHEN COUNT(*)>=2 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(total ORDER BY lid ASC SEPARATOR ' '),' ',2),' ',-1) END AS P2,
CASE WHEN COUNT(*)>=3 THEN SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(total ORDER BY lid ASC SEPARATOR ' '),' ',3),' ',-1) END AS P3
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 ORDER BY lid ASC;
我在一个等于或大于 2 的 GROUP_CONCAT
值上使用了两次 SUBSTRING_INDEX
函数,但在这个例子中我在计数为 3 时停止。我还在 GROUP_CONCAT
中添加了 ORDER BY lid ASC
以确保它将 return 按 lid
.