PHP MySQL -更改表以显示与街道地址匹配的同一行上的记录

toiithl6  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(97)

早上好,我需要一些帮助,真的很感激。我有一个表,看起来像下面这样:

但我希望它看起来像这样,以便用户更容易阅读:

我怎么把它变成这样?下面是当前代码:

<?php

$sql = "SELECT
companyname,
ordernumber,
equipment,
theserial,
type,
theaddress
FROM table";

$result = mysqli_query($con, $sql);

?>

<table id="moreinfotable" name="moreinfotable" class="table table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>OrderNumber</th>
<th>Equipment</th>
<th>Serial</th>
<th>Type</th>
<th>Address</th>

</tr>
</thead>

<?php

while($row = mysqli_fetch_array($result)) { 

$companyname = $row['companyname'];
$ordernumber = $row['ordernumber'];
$equipment = $row['equipment'];
$theserial = $row['theserial'];
$type = $row['type'];
$theaddress = $row['theaddress'];

echo '<tr>';
echo '<td>' . $companyname . '</td>';
echo '<td>' . $ordernumber . '</td>';
echo '<td>' . $equipment . '</td>';
echo '<td>' . $theserial . '</td>';
echo '<td>' . $type . '</td>';
echo '<td>' . $theaddress . '</td>';
echo'</tr>';

} 

?>

</table>
pprl5pva

pprl5pva1#

对于一个固定的类型列表(这里是新的交付和拾取),您可以使用条件聚合直接在SQL中透视数据集:

select companyname, address,
    max(case when type = 'New Delivery' then ordernumber end) as delivery_ordernumber,
    max(case when type = 'New Delivery' then equipment   end) as delivery_equipment,
    max(case when type = 'New Delivery' then serial      end) as delivery_serial,
    max(case when type = 'Pickup'       then equipment   end) as pickup_equipment,
    max(case when type = 'Pickup'       then serial      end) as pickup_serial
from mytable
group by companyname, address
kadbb459

kadbb4592#

我不确定,但你有没有试着订购你的查询?例如:SELECT * FROM exam ORDER BY exam_date;这意味着从检查中选择所有并按检查日期排序。

"SELECT
companyname,
ordernumber,
equipment,
theserial,
type,
theaddress
FROM table 
ORDER BY theaddress"

相关问题