简体   繁体   English

从选择查询中的两个不同表中减去两个不同的列总和

[英]subtract two different sum of column from two different table in select query

I have two tables with the name of add_sell and add_expense. 我有两个表,名称分别为add_sell和add_expense。 I have inserted some of value into the tables. 我已经在表中插入了一些价值。 I wanted to get the total profit result. 我想获得总利润结果。 I did it myself but the result is showing 0 . 我自己做了,但结果显示为0。 Now how can I do that. 现在我该怎么做。 I also give the total profit formula. 我还给出了总利润公式。 Is anyone here to help me? 有人在这里帮助我吗? Here is the code given below 这是下面给出的代码

function totalSell(){
    global $conn;
    $sql = "SELECT SUM(`sell_amount`) as 'Sumearning' FROM `add_sell` WHERE sell_amount = `sell_amount`";
        $result = $conn->query($sql);
        $getDate = date('d/m/Y');                    
        if(mysqli_num_rows($result) > 0){

        while($fetch = mysqli_fetch_array($result)){                  
        echo "SR ".$fetch['Sumearning']; 
       }
    }
}

function totalExpense(){
    global $conn;
    $ex = "SELECT SUM(`expense_amount`) as `Sumexpense` FROM `add_expense` WHERE expense_amount = `expense_amount` ";
    $run_ex = $conn->query($ex);
    if (mysqli_num_rows($run_ex) > 0) {
        while ($tex = mysqli_fetch_array($run_ex)) {
        echo "SR ".$tex['Sumexpense'];
        }
    }
}

function totalProfit(){ 函数totalProfit(){

    global $conn;

    $sql = "SELECT SUM(`sell_amount`) as 'Sumearning' FROM `add_sell` WHERE sell_amount = `sell_amount`";
        $result = $conn->query($sql);
        $getDate = date('d/m/Y');
        while($fetch = mysqli_fetch_array($result)){                  
            $fetch['Sumearning']; 
            echo "Total Earning : ".$totalEarning = "SR ".$fetch['Sumearning']; 
       }

$ex = "SELECT SUM(`expense_amount`) as `Sumexpense` FROM `add_expense` WHERE expense_amount = `expense_amount` ";
        $run_ex = $conn->query($ex);

        while ($tex = mysqli_fetch_array($run_ex)) {
            $tex['Sumexpense'];
            echo "Total Expense : ".$totlaExpenss = "SR ".$tex['Sumexpense'];
        }
        echo "Your totla Profit is : ".$totalprofit = $totalEarning - $totlaExpenss;
}

Do you mean getting the difference between the Sum earnings and the Sum expenses? 您是说要获得总收入和总支出之间的差额吗? Because you can do it like this? 因为可以这样吗?

$Total = $tex['Sumexpense'] - $fetch['Sumearning']; $ Total = $ tex ['Sumexpense']-$ fetch ['Sumearning'];

Echo $total; 总回声

Total profit result you can get from query itself. 您可以从查询本身获得总利润结果。

$sql = "SELECT SUM(`sell_amount`)-(SELECT SUM(`expense_amount`) FROM `add_expense` WHERE expense_amount = `expense_amount`)  AS `PROFITLOSS` FROM `add_sell` WHERE sell_amount = `sell_amount`";

Note: as Sumearning and as Sumexpense Removed instead you will get as PROFITLOSS 注意:作为SumearningSumexpense Removed,您将得到PROFITLOSS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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