[英]Getting Sum of db field using join active record in codeigniter
I have a table name products with all product details and another whs_products with quantity details of the products for each warehouse. 我有一个包含所有产品详细信息的表名产品和另一个包含每个仓库产品数量详细信息的whs_products。
i want select id, code and name from products table and sum of quantity where products.id = whs_products.product_id 我想要从产品表中选择id,代码和名称以及products.id = whs_products.product_id中的数量总和
I am trying this 我正在尝试这个
$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");
I getting the list products that exists in whs_products not the sum. 我得到whs_products中存在的列表产品而不是总和。 Some products are listed twice as they have 2 entries in whs_products. 有些产品列出两次,因为它们在whs_products中有2个条目。
I want list all the products once only where no quantity I want put 0 in quantity and where its is more than 1 in whs_products I want display sum of all the quantity 我想列出所有产品一次只有我想要的数量在数量上放0,在whs_products中它超过1我希望显示所有数量的总和
Help will be much appreciated! 将非常感谢帮助!
Table Structure 表结构
Products
id, code, name, unit, price
whs_products
id, product_id, warehouse_id, quantity
I have whs table too for warehouse id, name, address 仓库ID,名称,地址也有whs表
I tried this Sir, 我试过这个先生,
$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();
Every thing is fine. 一切顺利。 But the total number of products are calculated wrongly. 但产品总数计算错误。 I think system add 1 to total products, each time gets quantity from whs_products. 我认为系统在总产品中加1,每次从whs_products获取数量。 For some products quantity is 2 or 3 time depending on each warehouse. 对于某些产品,数量为2或3次,具体取决于每个仓库。
Any solutions for this. 任何解决方案。 I am very thankful for your support. 我非常感谢你的支持。
Please try out the following query and comment. 请尝试以下查询和评论。
Sample data:- 样本数据:-
Products 制品
PID PNAME
1 j
2 k
3 m
whs_Products whs_Products
WID PID QUANTITY
11 2 300
11 2 200
14 2 500
11 1 300
15 3 100
14 3 800
Query to get total by pid in whs_products
查询以whs_products
的pid获取总数
select pid, wid, sum(quantity)
from whs_products
group by pid, wid
;
Results: 结果:
PID WID SUM(QUANTITY)
1 11 300
2 11 500
2 14 500
3 14 800
3 15 100
query using a variable to get user input for pid
and by pid, wid 查询使用变量来获取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
;
Results: 结果:
PID PNAME WID SUM(A.QUANTITY)
2 k 11 500
2 k 14 500
final query to show quantity by user input pid only 最终查询仅显示用户输入pid的数量
Query: 查询:
-- 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
;
Results: 结果:
PID PNAME SUM(A.QUANTITY)
2 k 1000
Here is a headstart for you to try. 这是您尝试的先行者。 At first I had the impression you already know the syntax of codeigniter
and you are looking for SQL logic, so you could convert it into the desired format you need. 起初我的印象是你已经知道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();
Or write a funciton :) : 或写一个功能:):
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
in comments 在评论中编辑为LEFT JOIN
请求的OP To show all products in Products table, do the following: 要显示Products表中的所有产品,请执行以下操作:
In select
show pid
from Products
table. 在Products
表的select
show pid
中。
Use from Products Left Join Whs_Products
使用from Products Left Join Whs_Products
Group by pid
from Products
table 来自Products
表Group by pid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.