简体   繁体   中英

mysql_fetch_array not display results when i use join table in php

I have done a software like invoice. Now I want to display the specific result of stock. Such as (Total buy-Total sell=Stock) for each product which was buy and sell in many many times. I used below code when i use one table and test sum function for sell or bye it work but multi table not work?

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

}

In the inner join you need specify the inner condition to match rows between from the main table and the other tables. In your case:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM