简体   繁体   中英

How to join and get sum from three different tables in codeigniter?

I have a case like this, I want to make a report from three different tables in CodeIgniter, and then make a summary from other tables, for more detail check this out :

I've table tbl_product :

+----------+----------+---------+
| id_prdct | product  | remark  |
+----------+----------+---------+
|    1     | marriage | remark1 |
|    2     | office   | remark2 |
|    3     | school   | remark3 |
+----------+----------+---------+

then I've tables tbl_sell like below :

+---------+----------+------------+------------+
| id_sell | id_prdct | item_sell  | price_sell |
+---------+----------+------------+------------+
|    1    |    1     | dress      | USD 500    |
|    2    |    1     | shoes      | USD 600    |
|    3    |    1     | decoration | USD 1500   |
|    4    |    2     | dress      | USD 300    |
|    5    |    2     | shoes      | USD 500    |
|    6    |    3     | shoes      | USD 900    |
+---------+----------+------------+------------+

and tbl_buy like below :

+--------+----------+------------+------------+
| id_buy | id_prdct | item_buy   | price_buy  |
+--------+----------+------------+------------+
|    1   |    1     | dress      | USD 250    |
|    2   |    1     | shoes      | USD 300    |
|    3   |    1     | decoration | USD 1000   |
|    4   |    2     | dress      | USD 200    |
|    5   |    2     | shoes      | USD 300    |
|    6   |    3     | shoes      | USD 500    |
+--------+----------+------------+------------+

In my web page, I want to make a result like below :

+----+----------+------------+----------+----------+
| No | Product  | Sum Sell   | Sum Buy  | Profit   |
+----+----------+------------+----------+----------+
|  1 | marriage | USD 2600   | USD 1550 | USD 1050 |
|  2 | office   | USD 800    | USD 500  | USD 300  |
|  3 | school   | USD 900    | USD 500  | USD 400  |
+----+----------+------------+----------+----------+

so I want to make a summary from tbl_sell and tbl_buy , and then show them on my web page then make a summary (profit) as my last table in above. is there any suggestion to solve my case?

Thanks...

========================================================== [UPDATE ANSWER]

I already tried this but still got error, any suggestion for this code?

$d['data'] = $this->db->query("select sum(tbl_sell.price) AS total_sell, sum(tbl_buy.price) AS total_buy
            left join tbl_sell on tbl_sell.id_prdct=tbl_product.id_prdct
            left join tbl_buy on tbl_buy.id_prdct=tbl_product.id_prdct
            from tbl_product 
            WHERE tbl_sell.id_prdct=tbl_product.id_prdct AND tbl_buy.id_prdct=tbl_product.id_prdct ");

Try this sql:

SELECT p.id_prdct, 
       p.product, 
       sell.total, 
       buy.total 
FROM   tbl_product p 
       LEFT JOIN (SELECT id_prdct, 
                         Sum(price_sell) total 
                  FROM   tbl_sell 
                  GROUP  BY id_prdct) sell 
              ON p.id_prdct = sell.id_prdct 
       LEFT JOIN (SELECT id_prdct, 
                         Sum(price_buy) total 
                  FROM   tbl_buy 
                  GROUP  BY id_prdct) buy 
              ON p.id_prdct = buy.id_prdct; 

try this i hope it will help you.

function functionName($product_id){
$this->db->select('tbl_product.*,sum(tbl_sell.price_sell),sum(tbl_buy.price_buy)');
$this->db->join('tbl_sell','tbl_sell.id_prdct=tbl_product.id_prdct');
$this->db->join('tbl_buy','tbl_product.id_prdct=tbl_buy.id_prdct');
return $this->db->where('tbl_sell.id_prdct',$product_id)->where('tbl_buy.id_prdct',$product_id)->get('tbl_product')->result_array();
}

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