2013-01-04 40 views
0

我正試圖讓totalQuantity小於alterQuantity的產品。我可以在where子句中使用select域中的somename作爲somename嗎?

$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"); 

我得到的錯誤,在未知列'alertQuantity「where子句」如果我alertQuantity在where子句我得到未知列「totalQuantity」 products.alert_quantity「where子句」這一個我不知道如何改變,因爲這是數量的總和。你能幫我解決這個問題嗎?

我的問題: 我有選擇alert_quantity爲alertQuantity和金額(數量)爲totalQuantity

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

表結構

產品

ID,代碼,名稱,單元價格,alert_quantity

warehouses_products

ID,PRODUCT_ID,warehouse_id,數量


找到 我不能使用totalQuantity有沒有其他的想法,我怎麼能比較products.alert_quantity與sum(warehouses.products.quantity)?

回答

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 
+0

您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在'group_by products.id'附近使用正確的語法。ORDER BY(select product,'code','products'.'name','' – Saleem

+0

在我的查詢中沒有在GROUP by Products.id之後的任何封閉括號,請粘貼整個查詢以及訂單條款 –

+0

對不起,這是錯誤 - 'where子句'中的未知列'alertQuantity' select products.id as productid,products.code作爲代碼,products.name,products.unit,products.price,sum(warehouses_products.quantity)as totalQuantity,products.alert_quantity as alertQuantity from products LEFT join warehouses_products on products.id = warehouses_products.product_id where alertQuantity> = totalQuantity GROUP BY產品。編號 – Saleem

0

嘗試使用選擇第二個參數,如 變化

$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"); 
+0

嘗試過相同的錯誤'where子句'中的未知列'alertQuantity' – Saleem

+0

@Saleem嘗試更改您的Where子句,如在答案 –

+0

中添加的代碼已經嘗試過此:(但相同的問題是它不允許我使用alertQuantiy和totalQuantity,我選擇使用 – Saleem

相關問題