donglun1918 2014-08-24 20:16
浏览 14
已采纳

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

enter image description here

i use this table, and i was computing for the total of the SUM(quantity * price) but now i dont know how to use it for the specific serial from the customers table.

here is my query

<?php 

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

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

i was using this but i didnt really understand it, i created the new query because SUM() messes up the old one, i dont kno why, but it shows 1 row only and no more.

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

how do i join my tables and use the where clause.

  • 写回答

1条回答 默认 最新

  • dqqfuth6736 2014-08-24 20:25
    关注

    This might do the trick

     $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'];
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计