我有一个包含所有产品详细信息的表名 products 和另一个包含每个仓库产品数量详细信息的 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 次,具体取决于每个仓库。
任何解决方案。我非常感谢您的支持。