当我在php中使用连接表时,mysql_fetch_array不显示结果

时间:2016-02-13 07:09:08

标签: php mysql

我做了一个类似发票的软件。现在我想显示库存的具体结果。例如(每次买卖 - 总卖出=股票)多次买入和卖出的产品。我使用下面的代码,当我使用一个表和测试和功能出售或再见它工作,但多表不起作用?

buy:
id  name     detail    pquantity  unitprice
1   Laptop   Samsung      3         $150
2   Keyboard  Perfect     6          $5
3   Monitor   dell        2          $60
4   Laptop   Samsung      2          $150

sell
id  name     detail    iquantity  unitprice
1   Laptop   Samsung      1         $180
2   Keyboard  Perfect     1          $6
3   Laptop   Samsung      2          $170


//mysql query
$result= mysql_query("SELECT pname,pdetails, SUM(pquantity) - SUM(iquantity) AS stock FROM
buy INNER JOIN sell GROUP BY pname,pdetails");

//for display 
while ($row=mysql_fetch_array($result)){
 $itemname=$row['pname']; 
 $details=$row['pdetails']; 
 $pquantity=$row['pquantity']; 
 $iquantity =$row['iquantity']; 
 $cstock =$row['stock']; 
 echo "<tr>";

echo "<td>$itemname</td>"; 
echo "<td>$details</td>"; 
echo "<td>$pquantity</td>"; 
echo "<td>$iquantity</td>"; 
echo "<td>$cstock</td>"; 
echo "</tr>";

}

1 个答案:

答案 0 :(得分:0)

inner join中,您需要指定内部条件以匹配主表和其他表之间的行。在你的情况下:

SELECT pname, pdetails, SUM(pquantity) - SUM(iquantity) AS stock FROM
    buy b INNER JOIN sell s on s.id = b.id GROUP BY pname, pdetails;