我正在制作我的产品页面,其中我有许多产品具有相同的名称,但规格和价格不同。
PHP代码:
<code>
$SelectList = "SELECT a.p_id, a.dis,a.name,b.p_id,b.rate
FROM tst1 a
INNER JOIN max b
ON a.p_id = b.p_id GROUP BY name";
$Query = mysql_query($SelectList) or die(mysql_error());
$Num_rows = mysql_num_rows($Query);
$array = array();
while($Data=mysql_fetch_assoc($Query))
{
$array['name'][] = $Data['name'];
$array['rate'][] = $Data['rate'];
$array['dis'][] = $Data['dis'];
}
</code>
<code>
<table border="1">
<thead>
<th>Description</th>
<th>Unit</th>
<th>Qty</th>
<th>Rate</th>
</thead>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>65 mm</td>
<td>Nos</td>
<td>15</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>85 mm</td>
<td>Nos</td>
<td>45</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">WASHER VALVES</td>
</tr>
<tr>
<td>32 mm</td>
<td>Nos</td>
<td>95</td>
<td>30%</td>
</tr>
</table>
</code>
所以我想要的是,如果产品名称相同,那么只需获取&#34; name&#34;曾经,它的单位,数量和数量率&#34;所有这些。
<code>
<table style="position:relative; top:10px; bottom:10px; left:200px" border="1">
<thead>
<th>Description</th>
<th>Unit</th>
<th>Qty</th>
<th>Rate</th>
</thead>
<tr>
<td colspan="0">BUTTERFLY VALVES</td>
</tr>
<tr>
<td>65 mm</td>
<td>Nos</td>
<td>15</td>
<td>40%</td>
<td>85 mm</td>
<td>Nos</td>
<td>45</td>
<td>40%</td>
</tr>
<tr>
<td colspan="0">WASHER VALVES</td>
</tr>
<tr>
<td>32 mm</td>
<td>Nos</td>
<td>95</td>
<td>30%</td>
</tr>
</table>
</code>
答案 0 :(得分:0)
检查这个subquery
..这会帮助你..
$qry = mysqli_query("select * from product group by name");
while($row = mysqli_fetch_array($qry))
{
echo "<tr><td colspan="0">'".$row['name']."'</td></tr>";
$qry2 = mysqli_query("select * from product where name = '".$row['name']."'");
echo "<tr>";
while($row2 = mysqli_fetch_array($qry2))
{
echo "<td>'".$row2['mm']."'</td><td>'".$row2['nos']."'</td><td>'".$row2['qty']."'</td><td>'".$row2['percentage']."'</td>";
}
echo "</tr>";
}
答案 1 :(得分:-1)
您可以在SQL中使用distinct
或group by
运算符来执行此操作。
distinct
的用法如下所示:
SELECT DISTINCT column_name,column_name
FROM table_name;
您可以查看distinct
的{{3}}示例。
group by
的用法如下所示:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
以下是group by
的{{3}}。
我希望这会有所帮助。