在同一个表中加入同一列——MYSQL

时间:2022-02-03 01:07:49

I have this tables:

我有这个表:

TABLE addonlist_final

表addonlist_final

在同一个表中加入同一列——MYSQL

TABLE addons

表插件

在同一个表中加入同一列——MYSQL

First, I have to JOIN the same addon_id so I can get all the needed details(which I've already done). Then I want to JOIN the column with the same identity(addon_id) in the table then add the quantity to each other.

首先,我必须加入相同的addon_id,以便获得所需的所有细节(我已经这样做了)。然后,我要在表中加入具有相同标识(addon_id)的列,然后互相添加数量。

So far I have this code:

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

    <?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>";

?>

This shows me this:

这说明我这个:

在同一个表中加入同一列——MYSQL

What I want to show is:

我想展示的是:

在同一个表中加入同一列——MYSQL

Is this possible with just one query? Thanks in advance :)

只有一个查询可能吗?提前谢谢:)

1 个解决方案

#1


4  

You uave to use group by

你必须使用团体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

#1


4  

You uave to use group by

你必须使用团体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