0

我正在尝试获取 totalQuantity 小于 alterQuantity 的产品。

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

如果我在 where 子句中使用 products.alert_quantity 警告数量,我在“where 子句”中收到未知列“alertQuantity”的错误我在“where 子句”中得到未知列“totalQuantity”, 我不知道如何更改,因为这是数量之和。你能帮我解决这个问题吗?

我的问题: 我选择 alert_quantity 作为 alertQuantity 和 sum(quantity) 作为 totalQuantity

我可以在 where 子句中使用 alterQuantity 和 totalQuantity 吗?

表结构

产品

id、代码、名称、单位、价格、alert_quantity

仓库_产品

id、product_id、warehouse_id、数量


发现 我不能使用 totalQuantity 有没有其他想法可以将 products.alert_quantity 与 sum(warehouses.products.quantity) 进行比较?

4

2 回答 2

0

尝试这个::

select 
products.id as productid, 
products.code as code, 
products.name, products.unit, 
products.price, 
sum(products.alert_quantity) as totalQuantity, 
products.alert_quantity as alertQuantity
from products

LEFT join warehouses_products on products.id=warehouses_products.product_id
where products.alert_quantity>= totalQuantity
group_by products.id
于 2013-01-04T09:00:49.797 回答
0

尝试使用 select 的第二个参数,例如更改

$this->db
->select("products.id as productid, products.code as code, 
products.name, products.unit, products.price, 
sum(warehouses_products.quantity) as totalQuantity, 
products.alert_quantity as alertQuantity")

$this->db
->select("products.id as productid, products.code as code, 
products.name, products.unit, products.price, 
sum(warehouses_products.quantity) as totalQuantity, 
products.alert_quantity as alertQuantity", FALSE)

并关闭连接部分的引号

编辑,尝试重新排序您的语句,例如:

$this->db
->select("products.id as productid, products.code as code, products.name, products.unit, products.price, sum(warehouses_products.quantity) as totalQuantity, products.alert_quantity as alertQuantity", FALSE)
->from('products')
->join('warehouses_products', 'products.id=warehouses_products.product_id', 'left')
->where('alertQuantity >=', 'totalQuantity')
->group_by("products.id");
于 2013-01-04T09:05:18.880 回答