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 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误