在一列中放置多个 MySQL 行
Putting more than one MySQL Row in a column
我遇到的问题是,我的数据库中有一个 Table,我想在一列中获取此 table 的一些行。
例如我有这个 table 名字 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 循环获取这个 table 我会得到相同的 table.
所以我想要的是这样的table:
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
| 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 | | | | |
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
此 table 列有名称列和从 1 到 10 的可能 LNr 的枚举。 LNr 列中的数据是上面 table 中的 LID。
我的问题是,如何将名称和 LNr、LID 放在右列中?
在此 table 中,您已列出所有名称并为所有 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);
?>
我希望你明白我的意思和想要做的。如果没有,请随时询问。
谢谢
我不确定我是否理解正确,但我认为这个查询应该非常接近您要执行的操作:
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" 列,并为您提供我喜欢称之为 "stairway" 结果集的内容。我们现在要做的就是展平它,这就是外部查询通过为每一行选择 max(LNr) 并按名称分组所做的事情。它之所以有效,是因为默认情况下实际值总是大于空值,因此为每列选择最大值将摆脱空值并为您提供实际值。
你可以这样写查询
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;
希望对您有所帮助。
从这个查询开始
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) 和行 (Name)。
$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>";
我遇到的问题是,我的数据库中有一个 Table,我想在一列中获取此 table 的一些行。
例如我有这个 table 名字 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 循环获取这个 table 我会得到相同的 table.
所以我想要的是这样的table:
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
| 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 | | | | |
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
此 table 列有名称列和从 1 到 10 的可能 LNr 的枚举。 LNr 列中的数据是上面 table 中的 LID。
我的问题是,如何将名称和 LNr、LID 放在右列中? 在此 table 中,您已列出所有名称并为所有 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);
?>
我希望你明白我的意思和想要做的。如果没有,请随时询问。 谢谢
我不确定我是否理解正确,但我认为这个查询应该非常接近您要执行的操作:
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" 列,并为您提供我喜欢称之为 "stairway" 结果集的内容。我们现在要做的就是展平它,这就是外部查询通过为每一行选择 max(LNr) 并按名称分组所做的事情。它之所以有效,是因为默认情况下实际值总是大于空值,因此为每列选择最大值将摆脱空值并为您提供实际值。
你可以这样写查询
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;
希望对您有所帮助。
从这个查询开始
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) 和行 (Name)。
$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>";