在同一个表中加入相同的列 - MYSQL

时间:2015-01-27 13:20:35

标签: php mysql

我有这张表:

addonlist_final

enter image description here

插件

enter image description here

首先,我必须JOIN相同addon_id所以我可以获得所有必需的详细信息(我已经完成了)。然后,我想在JOINcolumn table <?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>"; ?> 具有相同身份(addon_id),然后将数量相互添加。

到目前为止,我有这段代码:

{{1}}

这告诉我这个:

enter image description here

我想表现的是:

enter image description here

只需一个查询就可以实现吗?在此先感谢:)

1 个答案:

答案 0 :(得分:4)

您必须使用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