簡體   English   中英

使用codeigniter中的連接活動記錄獲取db字段的總和

[英]Getting Sum of db field using join active record in codeigniter

我有一個包含所有產品詳細信息的表名產品和另一個包含每個倉庫產品數量詳細信息的whs_products。

我想要從產品表中選擇id,代碼和名稱以及products.id = whs_products.product_id中的數量總和

我正在嘗試這個

$this->db->select("id, code, name");
$this->db->from("products");
$this->db->join('whs_products', 'products.id = whs_products.product_id');
$this->db->select("quantity");

我得到whs_products中存在的列表產品而不是總和。 有些產品列出兩次,因為它們在whs_products中有2個條目。

我想列出所有產品一次只有我想要的數量在數量上放0,在whs_products中它超過1我希望顯示所有數量的總和

將非常感謝幫助!

表結構

Products
id, code, name, unit, price

whs_products
id, product_id, warehouse_id, quantity

倉庫ID,名稱,地址也有whs


我試過這個先生,

$this->db->select("products.id as productid, products.code, products.name, products.unit, products.cost, products.price,   sum(whs_products.quantity) as 'totalQuantity'")
->from('products')
->join('whs_products', 'whs_products.product_id=products.id', 'left')
->group_by("products.id");
$this->db->get();

一切順利。 但產品總數計算錯誤。 我認為系統在總產品中加1,每次從whs_products獲取數量。 對於某些產品,數量為2或3次,具體取決於每個倉庫。

任何解決方案。 我非常感謝你的支持。

請嘗試以下查詢和評論。

樣本數據:-

制品

PID     PNAME
1   j
2   k
3   m

whs_Products

WID     PID     QUANTITY
11  2   300
11  2   200
14  2   500
11  1   300
15  3   100
14  3   800

查詢以whs_products的pid獲取總數

select pid, wid, sum(quantity) 
from whs_products
group by pid, wid
;

結果:

PID     WID     SUM(QUANTITY)
1       11      300
2       11      500
2       14      500
3       14      800
3       15      100

查詢使用變量來獲取pid和pid,wid的用戶輸入

-- group by pid and wid
set @var:='2'
;
select a.pid, b.pname, a.wid, sum(a.quantity) 
from whs_products a
join products b
on b.pid = a.pid
where a.pid = @var
group by a.pid, wid
;

結果:

PID     PNAME   WID     SUM(A.QUANTITY)
2       k   11  500
2       k   14  500

最終查詢僅顯示用戶輸入pid的數量

查詢:

-- by pid only    
set @var:='2'
;
select a.pid, b.pname, sum(a.quantity) 
from whs_products a
join products b
on b.pid = a.pid
where a.pid = @var
group by a.pid
;

結果:

PID     PNAME   SUM(A.QUANTITY)
2       k   1000

因為OP需要CodeIgniter

這是您嘗試的先行者。 起初我的印象是你已經知道codeigniter的語法,並且你正在尋找SQL邏輯,所以你可以將它轉換成你需要的所需格式。

$this->db->select("a.pid, b.pname, count(a.quantity) as 'toalQuantity'");
$this->db->from('wsh_products a');
$this->db->join('products b', 'a.pid=b.pid', 'inner');
$this->db->group_by("a.pid"); 
$where = "a.pid = 2";
$this->db->get();
$query->results_array();

或寫一個功能:):

function getQuantity($prodid = false)
{
  $this->db->select(a.pid, b.pname, count(a.quantity) as 'toalQuantity');
  $this->db->join('wsh_products a', 'a.pid=b.pid');
  if ($prodid !== false)
    $this->db->where('a.pid', $prodid);
  $query = $this->db->get('products b');

  if($query->result() == TRUE)
  {
    foreach($query->result_array() as $row)
    {
      $result[] = $row;
    }
    return $result;
  }
}

在評論中編輯為LEFT JOIN請求的OP

要顯示Products表中的所有產品,請執行以下操作:

  • Products表的select show pid中。

  • 使用from Products Left Join Whs_Products

  • 來自ProductsGroup by pid

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM