【发布时间】:2016-03-24 23:45:01
【问题描述】:
我想用 laravel 写这个查询,
我的表结构是
- 产品表
Product_Id int(10)
产品名称 varchar(100)
Product_Description varchar(300)
Category_ID int(11)
Brand_ID int(11)
2.Stock_IN
stock_ID int(10)
Product_ID int(11)
Product_Count int(11)
Order_No int(11)
Supplier_ID int(11)
订单日期文本
- stock_out
stock_out_id int(10)
Product_ID int(11)
Product_Count int(11)
Requisition_No int(11)
Requisition_By varchar(100)
收件人 varchar(100)
我的查询:
SELECT X.Product_ID,X.Product_Name,X.Count_StockIN,IFNULL(Y.Count_StockOut,0) Count_StockOut,(X.Count_StockIN-IFNULL(Y.Count_StockOut,0)) Balance FROM ( SELECT M.product_ID,M.Product_Name, SUM(Product_Count) AS Count_StockIN from (SELECT A.*,B.Product_Name FROM `stock_in` A, `products` B where A.Product_ID=B.Product_ID )M group by Product_ID,Product_Name ) X LEFT JOIN ( SELECT product_ID, SUM(Product_Count) AS Count_StockOUT from `stock_Out`group by Product_ID ) Y On X.Product_ID=Y.Product_ID
输出是:
如何在 laravel 4.2 中编写此查询。请帮帮我。
【问题讨论】:
-
那么...您使用的是 eloquent 还是查询构建器?你有没有尝试过?
-
您可以使用查询生成器laravel.com/docs/4.2/queries#raw-expressions ,您可以使用count like count(1) 来获取group_by 记录数
-
我尝试使用查询生成器,但无法完成。
-
谢谢大家,感谢你们的潜在努力。问题解决了。