多个表mysql问题的总和

时间:2016-04-22 07:37:45

标签: php mysql

$nstkquty=$db->execute("SELECT (SUM(p.quantity) - SUM(s.quantity) -
                                SUM(o.quantity) - SUM(pr.quantity)) AS totalquty 
                        FROM master_purchase AS p, product_sales AS s,
                             orders_details AS o, purchase_return AS pr 
                        WHERE p.product_code='" . $cod . "' && 
                              s.prd_code='" . $cod . "' && 
                              o.prd_code='" . $cod . "' &&  
                              pr.product_code='" . $cod . "'");
$nwrquty = $nstkquty->fetch_assoc();
$data['avalqty'] = $nwrquty[0] ['SUM(totalquty)']; //$total_stkqty;
echo json_encode($data);
exit();

这是我的代码,没有显示结果,没有错误,也没有显示警告。任何人都帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

未使用 $ data [' avalqty'] = $ nwrquty [0] [' SUM(totalquty)'];

使用像 $ data [' avalqty'] = $ nwrquty [0] [' totalquty'];

答案 1 :(得分:0)

更新您的查询:

$nstkquty=$db->execute("SELECT ( sum(if(p.quantity is null, 0, p.quantity)) - sum(if(s.quantity is null, 0, s.quantity)) -
                            sum(if(o.quantity is null, 0, o.quantity)) - sum(if(pr.quantity is null, 0, pr.quantity))) AS totalquty 
                    FROM master_purchase AS p, product_sales AS s,
                         orders_details AS o, purchase_return AS pr 
                    WHERE p.product_code='" . $cod . "' && 
                          s.prd_code='" . $cod . "' && 
                          o.prd_code='" . $cod . "' &&  
                          pr.product_code='" . $cod . "'");

同时将此行更新为:

$data['avalqty'] = $nwrquty[0] ['totalquty']; //$total_stkqty;