我遇到的问题是,我的数据库中有一个表,并希望在一列中获取该表的某些行。
例如,我将此表命名为klz:
|-------+-----------+-----+-----|
| ID | Name | LNr | LID |
|-------+-----------+-----+-----|
| 1 | 0000_01 | 1 | 16 |
| 2 | 0000_01 | 2 | 35 |
| 3 | 0000_02 | 1 | 16 |
| 4 | 0000_02 | 2 | 35 |
| 5 | 0000_10 | 1 | 18 |
| .. | .. | .. | .. |
| 297 | 0214_01 | 1 | 23 |
| 298 | 0214_01 | 1 | 66 |
| 299 | 0214_01 | 2 | 24 |
| 300 | 0214_01 | 2 | 67 |
| 301 | 0214_01 | 3 | 26 |
| 302 | 0214_01 | 4 | 28 |
| 303 | 0214_01 | 4 | 69 |
| 304 | 0214_01 | 5 | 30 |
| 305 | 0214_01 | 5 | 70 |
| 306 | 0214_01 | 6 | 31 |
| 307 | 0214_01 | 6 | 71 |
|-------+-----------+-----+-----|
如果我在PHP中使用while循环获取此表,我会得到相同的表。
所以我想要的是这样一个表:
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
| Name | LNr1 | LNr2 | LNr3 | LNr4 | LNr5 | LNr6 | LNr7 | LNr8 | LNr9 | LNr10 |
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
| 0000_01 | 16 | 35 | | | | | | | | |
| 0000_02 | 16 | 35 | | | | | | | | |
| 0000_10 | 18 | | | | | | | | | |
| 0214_01 | 23 - 66 | 24 | 26 - 68 | 28 - 69 | 30 - 70 | 31 - 71 | | | | |
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
此表具有列名称和可能的LNr的枚举,从1到10作为列。 LNr列中的数据是上表中的LID。
我的问题是,我如何依赖名称和LNr,右栏中的LID? 在此表中,您列出了所有名称一次,并为所有LNr设置了正确的LID。
以下是我正在尝试执行此操作的代码...直到现在它仅适用于第一个LNr列,将LID写入:
<?php
include "dbconnect.php";
$klz = mysqli_query($db, "SELECT *
FROM klz;");
?>
<div class="container-fluid">
<div class="row">
<div class="table-responsive">
<div class="col-xs-12">
<table id="grid-klz" class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th> Name </th>
<th> Leitungsnr 1</th>
<th> Leitungsnr 2</th>
<th> Leitungsnr 3</th>
<th> Leitungsnr 4</th>
<th> Leitungsnr 5</th>
<th> Leitungsnr 6</th>
<th> Leitungsnr 7</th>
<th> Leitungsnr 8</th>
<th> Leitungsnr 9</th>
<th> Leitungsnr 10</th>
</tr>
</thead>
<tbody>
<?php
$name_old = "";
$lnr_old = "";
$name = [];
$lnr = [];
$lid = [];
while($row = mysqli_fetch_array($klz, MYSQL_ASSOC)){
$name[] = $row['Name'];
$lnr[] = $row['LNr'];
$lid[] = $row['LID'];
}
for($i=0; $i <= sizeof($name)-1; $i++){
if($name[$i] != $name_old){
echo "<tr>";
echo "<td>". $name[$i] . "</td> \n";
if($lnr[$i] != $lnr_old){
echo "<td>". $lid[$i] . "</td> \n";
$lnr_old != $lnr[$i];
}
echo "</tr>";
$name_old = $name[$i];
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
mysqli_close($db);
?>
我希望你知道我的意思和尝试。如果没有随意请问。 谢谢
答案 0 :(得分:2)
从此查询开始
SELECT
Name AS tableRows,
LNr AS tableCols,
GROUP_CONCAT(LID ORDER BY LID SEPARATOR ' - ') AS cellValue
FROM klz
GROUP BY 1,2
ORDER BY 1,2;
现在将结果放入数组中。 此外,获取其他数组中的每个不同列(LNr)和行(名称)。
$ar = array();
$tableRows = array();
$tableCols = array();
while($row = mysqli_fetch_array($klz, MYSQL_ASSOC)){
$ar[$row['tableRows']][$row['tableCols']] = $row['cellValue'];
$tableRows[] = $row['tableRows'];
$tableCols[] = $row['tableCols'];
}
$tableRows = array_unique($tableRows);
$tableCols = array_unique($tableCols);
使用行和列对2个数组进行排序
sort($tableRows);
sort($tableCols);
foreach行,foreach列,多数组的回显值
echo "<table>";
echo "<tr><th>Name</th>";
foreach($tableCols as $x){
echo "<th>LNr$x</th>";
}
echo "</tr>";
foreach($tableRows as $y){
echo "<tr><td>$y</td>";
foreach($tableCols as $x){
echo "<td>" . $ar[$y][$x] . "</td>";
}
echo "</tr>";
}
echo "</table>";
答案 1 :(得分:1)
我不确定我是否理解正确,但我认为此查询应该与您尝试的内容非常接近:
select name,
max(LNr1) as LNr1,
max(LNr2) as LNr2,
...
max(LNr10) as LNr10
from
(
select name,
if(LNr = 1, LID, null) as LNr1,
if(LNr = 2, LID, null) as LNr2,
...
if(LNr = 10, LID, null) as LNr10
from klz
) res
group by name
内部部分将在不同的“LNr”列中拆分行,并为您提供我称之为“阶梯”结果集的内容。我们现在要做的就是将它展平,这是外部查询通过为每行选择max(LNr)并按名称分组来完成的。它的工作原理是因为默认情况下实际值总是大于null,因此选择每列的最大值将除去空值并为您提供实际值。
答案 2 :(得分:1)
你可以写这样的查询
SELECT a.name,
GROUP_CONCAT(LNr1.LID SEPARATOR '-') AS LNr1,
GROUP_CONCAT(LNr2.LID SEPARATOR '-') AS LNr2,
GROUP_CONCAT(LNr3.LID SEPARATOR '-') AS LNr3,
GROUP_CONCAT(LNr4.LID SEPARATOR '-') AS LNr4,
GROUP_CONCAT(LNr5.LID SEPARATOR '-') AS LNr5,
GROUP_CONCAT(LNr6.LID SEPARATOR '-') AS LNr6,
GROUP_CONCAT(LNr7.LID SEPARATOR '-') AS LNr7,
GROUP_CONCAT(LNr8.LID SEPARATOR '-') AS LNr8,
GROUP_CONCAT(LNr9.LID SEPARATOR '-') AS LNr9,
GROUP_CONCAT(LNr10.LID SEPARATOR '-') AS LNr10
FROM klz AS a
LEFT JOIN klz AS LNr1 ON LNr1.LNr = 1 AND a.id = LNr1.id
LEFT JOIN klz AS LNr2 ON LNr2.LNr = 2 AND a.id = LNr2.id
LEFT JOIN klz AS LNr3 ON LNr3.LNr = 3 AND a.id = LNr3.id
LEFT JOIN klz AS LNr4 ON LNr4.LNr = 4 AND a.id = LNr4.id
LEFT JOIN klz AS LNr5 ON LNr5.LNr = 5 AND a.id = LNr5.id
LEFT JOIN klz AS LNr6 ON LNr6.LNr = 6 AND a.id = LNr6.id
LEFT JOIN klz AS LNr7 ON LNr7.LNr = 7 AND a.id = LNr7.id
LEFT JOIN klz AS LNr8 ON LNr8.LNr = 8 AND a.id = LNr8.id
LEFT JOIN klz AS LNr9 ON LNr9.LNr = 9 AND a.id = LNr9.id
LEFT JOIN klz AS LNr10 ON LNr10.LNr = 10 AND a.id = LNr10.id
GROUP BY a.name;
希望这有帮助。