这又是THIS的扩展排名。
我更改了我的表格式,我的意思是我为vendor_locations创建了一个单独的表,其中我在多行中针对该供应商ID存储了多个位置。并创建了一个表vendor_products来存储该供应商的多个产品。
现在我的问题是,由于每个供应商的产品和位置都是多个,而显示我没有得到如何在一行中显示它。
如果产品为3且位置为2,则所有细节将显示三次。如果我删除任何位置或产品,它将正常工作。但我怎么能为这两个
做这个 if(isset($_POST['submit']))
{
$sql="SELECT vendor.id AS venid
, vendor.name AS VNAME
, vendor.category
, vendor.website
, vendor.email
, vendor.phone
, vendor.vat
, vendor.pan
, items.name AS iname
, location.name AS locname
, items.item_id
FROM vendor
INNER JOIN vendor_location ON vendor.id = vendor_location.vendor_id
INNER JOIN vendor_products ON vendor.id=vendor_products.vendor_id
INNER JOIN location ON vendor_location.location = location.loc_id
INNER JOIN items ON vendor_products.item_id=items.item_id
ORDER BY vendor.id";
}
$sql1 = mysql_query($sql) or die(mysql_error());
}
?>
<div class="w-box w-box-blue">
<div class="w-box-header">
<h4>Vendor</h4>
</div>
<div class="w-box-content">
<table id="dt_hScroll" class="table table-striped">
<thead><tr>
<th>Vendor ID</th>
<th>Vendor</th>
<th>Category</th>
<th>Website</th>
<th>Email</th>
<th>Phone</th>
<th>VAT</th>
<th>PAN</th>
<th>Products</th>
<th>Locations</th>
</tr>
</thead>
<tbody>
<?php
$current = ''; // STORE THE SUPPLIER SID
while($row = mysql_fetch_array($sql1)) {
if ($row['venid'] != $current) {
echo "<tr>
<td><a href='edit_vendor_details.php?id=$row[venid]'>{$row['venid']}</a></td>
<td>{$row['VNAME']}</td>
<td>{$row['category']}</td>
<td>{$row['website']}</td>
<td>{$row['email']}</td>
<td>{$row['phone']}</td>
<td>{$row['vat']}</td>
<td>{$row['pan']}</td>
";
$current = $row['venid']; // RESET STORED SID
}
else {
echo "<tr><td colspan='8'> </td>";
}
echo "<td>{$row['iname']}</td>";
echo "<td>{$row['locname']}</td>";
echo "</tr>";
}
echo('</tbody></table>');
?>
</tbody></table>
答案 0 :(得分:3)
group_concat
。像这样:
SELECT vendor.id AS venid --rest of selection
, GROUP_CONCAT(location.name) AS locname
, GROUP_CONCAT(items.item_id)
FROM vendor --rest of your joins
GROUP BY vendor.id
在此处详细了解:MySQL Aggregate Functions