如何从另一个表获取我的WHERE子句的特定ID?使用连接

时间:2014-08-24 20:16:49

标签: php mysql sum

enter image description here

我使用这个表,我正在计算SUM(数量*价格)的总和,但现在我不知道如何将它用于customers表中的特定序列。

这是我的查询

<?php 

$qry = "SELECT SUM(price * quantity), chever from order_detail";

$result = @mysql_query($qry);
while ($row=mysql_fetch_array($result)){
echo $row['chever'];
}
?>

我正在使用这个,但我并没有真正理解它,我创建了新的查询,因为SUM()弄乱了旧的,我不知道为什么,但它只显示1行而不再。

$qry = "SELECT
            order_detail.quantity,
            order_detail.price,
            customers.serial,
            customers.name,
            customers.payment,
            customers.carrier, 
            orders.date, 
            order_detail.productid,
            order_detail.quantity, 
            order_detail.price, 
            order_detail.orderid, 
            inventory.prod_name
        FROM 
            customers 
        RIGHT JOIN 
            orders on customers.serial=orders.serial 
        RIGHT JOIN 
            order_detail on orders.serial=order_detail.orderid 
        LEFT JOIN 
            inventory on order_detail.productid=inventory.prod_id 
        WHERE
            customers.serial='{$_GET['serial']}'";

mysql_set_charset("UTF8");
$result = @mysql_query($qry);
if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
echo "<div align='left'>";
echo "<table class='CSSTableGenerator' id=''>
<tr>


<td>Products</td>
<td>Quantity</td>
<td>Total Price</td>


<tr>";
while ($row=mysql_fetch_array($result)){
echo "<tr>";
//echo "<td>".$row['date']."</td>";
//echo "<td>".$row['name']."</td>";
//echo "<td>".$row['orderid']."</a></td>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".($row['price']*$row['quantity'])."</td>";
//echo "<td>".$row['payment']."</td>"
echo "</tr>";
    }
echo "</table>";

如何加入我的表并使用where子句。

1 个答案:

答案 0 :(得分:0)

这可能会成功

 $qry = "SELECT SUM(order_detail.price * order_detail.quantity) AS totalOD, customers.serial from order_detail
            LEFT JOIN orders ON orders.serial = order_detail.orderid
            RIGHT JOIN customers ON customers.serial = orders.serial
            WHERE
                customers.serial='{$_GET['serial']}'";


    $result = @mysql_query($qry);
    while ($row=mysql_fetch_array($result)){
    echo $row['totalOD'];
    }
    ?>