按两个具有相同ID的不同表的总和分组

时间:2020-09-21 12:41:13

标签: php mysql

我创建了一个简单的屠宰Web应用程序,该应用程序有两个表:买卖。

所购买的表将包括用户购买的每种肉的千克和价格,而出售表还将包括所销售的每种肉的千克和价格。

每种肉都有自己的ID,例如,如果用户购买了ID为2的山羊,则它将在肉ID列中插入,如果他们出售山羊,则将在ID栏中插入相同的ID出售表的肉ID

现在我要做的是创建一个简单的分析,当用户从复选框中选择月份时,它将为他们提供所选月份的总公斤数,购买价格和总公斤数,出售价格,因此用户可以看到他们为每种肉赚了多少利润,以及他们损失了多少公斤。

用户可以选择多个月并比较统计数据。例如,如果用户单击“四月”和“五月”复选框,则应为他们提供这两个月内买卖的总公斤数

购买表

Bought table

销售表

sell table

我的sql代码

public function getkiloleft($month){


  
  $month_filter = implode("','",  $month);

    
    $query = "
              WHERE sell.month IN('". $month_filter."')
              AND bought.month IN('". $month_filter."')
              GROUP BY bought.meat_id ,sell.m_id
              
         ";
   
   $this->db->query('SELECT
                      bought.id as b_id, 
                      sell.id as s_id , 
                      sell.name,
                      bought.name,

                      SUM(sell.sold) AS sold,
                      SUM(bought.bought) AS bought,


                      SUM(sell.s_kilo) AS sk,
                      SUM(bought.b_kilos)AS bk
                FROM sell
                inner JOIN bought ON bought.meat_id =  sell.m_id
                '.$query.'
        ');



        $results = $this->db->resultSet();
        return $results;

}

获取结果的代码

   $results = $this->FabsModel->getkiloleft($month);
  
      foreach ($results as $rs){

      $profit= $rs->sold - $rs->bought;


       $kilos= $rs->sk - $rs->bk;


        

    $data.="<p>
                      name of meat: <b> $rs->name</b>
                      <br><br>
                      bought for: ".number_format($rs->bought,1)." $<br>
                      kilos  bought: $rs->bk KG<br>
                      <br><br>

                      sold for :  $rs->sold $<br>
                      kilos sold:  ".number_format($rs->sk,1)." kg<br>
                      <br><br>
                      profit:    ".number_format($profit,1)." $<br>
                      kilos left:    $kilos KG
                          
                         </p>
                         
                         <hr>";
   }


echo $data;

这是我在四月检查时得到的结果

enter image description here

这是我在4月和5月检查时得到的结果

enter image description here

基于4月份的结果,它表示公斤和价格乘以两倍,而我不希望发生这种情况,当我检查4月和5月时,结果仍在继续乘以

我希望在四月支票上获得的结果应该是


 name of meat: goat

bought for: 20.1 $
kilos bought: 10.1 KG


sold for : 5 $
kilos sold: 3.0 kg


profit: -15.1 $
kilos left: 7.1 KG



name of meat: lamb

bought for: 11.0 $
kilos bought: 15.1 KG


sold for : 6.6 $
kilos sold: 4.6 kg


profit: -4.4 $
kilos left: 10.5 KG 

当我单击四月和五月时我想要的结果

 name of meat: goat

bought for: 22.1 $
kilos bought: 11.1 KG


sold for :  8.2$
kilos sold: 4.0 kg


profit: -13.9 $
kilos left: 7.1 KG



name of meat: lamb

bought for: 11.0 $
kilos bought: 15.1 KG


sold for : 11.6 $
kilos sold: 7.6 kg


profit: 0.6 $
kilos left: 7.5 KG 


1 个答案:

答案 0 :(得分:1)

这看起来似乎有些复杂。如果每个客户在两个表中都有多行,那么加入然后进行汇总将使金额成倍增加。您还必须考虑用户仅在两个表之一中具有匹配项的可能性。

我会推荐union all

select id, name, sum(sold) sold, sum(s_kilos) s_kilos, sum(bought) bought, sum(b_kilos) b_kilos
from (
    select meat_id id, name, sold, s_kilos, 0 bought, 0 b_kilos from sold
    union all select m_id, name, 0, 0, bought, b_kilos from bought
) t
where ...
group by id, name

在现实生活中,您将拥有肉类的参考表,可以left join来自以下位置:

select m.id, m.name, s.sold, s.s_kilos, b.bought, b.b_kilos 
from meats m
left join (
    select meat_id, sum(sold) sold, sum(s_kilos) s_kilos
    from sold
    group by meat_id
) s on s.meat_id = m.id
    left join (
    select m_id, sum(bought) bought, sum(b_kilos) b_kilos
    from bought
    group by m_id
) b on b.m_id = m.id