我有这个表:
表 addonlist_final
TABLE 插件
首先,我必须JOIN
相同的addon_id
,这样我才能获得所有需要的详细信息(我已经完成了)。然后我想JOIN
表
中具有相同标识(addon_id)的列
,然后将数量相加。
到目前为止我有这段代码:
<?php
include("conn.php");
echo "<table border='1' >";
echo "<tr>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Qty</th>";
echo "<th>Total Cost</th>";
echo "</tr>";
$totaldue = 0;
$currentaddons = mysql_query("SELECT a.*, af.*
FROM addons AS a, addonlist_final AS af
WHERE a.addon_id = af.faddon_id and af.ftransac_id='2685'
ORDER BY af.timef DESC
");
while($rows = mysql_fetch_assoc($currentaddons)){
$desc = $rows['description'];
$price = $rows['price'];
$qty = $rows['quantity'];
$totalcost = $price * $qty;
$totaldue += $price * $qty;
echo "<tr>";
echo "<td>$desc</td>";
echo "<td>$price</td>";
echo "<td>$qty</td>";
echo "<td>$totalcost</td>";
echo "</tr>";
}
echo "</table>";
echo "<h3>Total Due: ".number_format($totaldue)."</h3>";
?>
这向我展示了这一点:
我要展示的是:
仅通过一个查询就可以做到这一点吗?提前致谢:)
最佳答案
你必须使用group by
SELECT a.description, sum(af.quantity) as quantity,price
FROM addons AS a, addonlist_final AS af
WHERE a.addon_id = af.faddon_id and af.ftransac_id='2685'
group by a.description
ORDER BY af.timef DESC
关于php - JOIN 同一个表中的同一列 - MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28171536/