根据订单表的缺货订单查询订单详情,订单项目详情,产品详情,产品类别详情,总库存数量,以及某个仓库库存数量;以便生产和采购进行产品生产与采购。
并使用一个SQL语句完成查询;
Laravel框架版本5.8;
包含表格:订单表,订单详情表,产品详情表,产品类别表,产品库存表;
SELECT
order_code,
product_code,
cn_name,
tabel_b.product_id,
item_id,
self,
tabel_b.quantity,
in_quantity,
in_quantity_lock,
sum( CASE inven.warehouse_id WHEN 1 THEN inven.quantity ELSE 0 END ) dg_quantity
FROM
(
SELECT
order_code,
sku AS product_code,
cn_name,
tabel_a.product_id,
item_id,
self,
tabel_a.quantity,
sum( inventory.quantity ) in_quantity,
sum( inventory.quantity_lock ) in_quantity_lock
FROM
(
SELECT
orders.order_code,
product.sku,
cn_name,
items.product_id,
items.id AS item_id,
sum( CASE cate.category_id WHEN 26 THEN 1 ELSE 0 END ) self,
items.quantity
FROM
erp_orders_detail items
LEFT JOIN erp_orders orders ON items.order_id = orders.id
LEFT JOIN erp_product_category cate ON cate.product_id = items.product_id
LEFT JOIN erp_product product ON items.product_id = product.id
WHERE
orders.STATUS = 4
GROUP BY
item_id,
items.product_id
) tabel_a
LEFT JOIN erp_inventory inventory ON tabel_a.product_id = inventory.product_id
GROUP BY
item_id
) tabel_b
LEFT JOIN erp_inventory inven ON inven.product_id = tabel_b.product_id
GROUP BY
item_id
ORDER BY
self DESC
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query\Builder as QBuilder;
//上面是引用类,为区别于Eloquent\Builder,重命名为QBuilder
//下面的是构建查询的核心代码
//要点:用于构造子查询的临时表,无get方法
$temp_a=DB::table('erp_orders_detail as items')
->leftJoin('erp_orders as orders','items.order_id','=','orders.id')
->leftJoin('erp_product as product','product.id','=','items.product_id')
->leftJoin('erp_product_category as cate','cate.product_id','=','items.product_id')
->where('orders.status','=',4)
->selectRaw('orders.order_code,items.quantity as item_quantity,items.product_id,items.id as item_id,product.sku,product.cn_name,sum(case cate.category_id when 26 then 1 else 0 end) self')
->groupBy('items.id');
$temp_b=DB::table('erp_inventory as inventory')
->rightJoinSub($temp_a,'table_a',function(QBuilder $query){
$query->on('inventory.product_id','=','table_a.product_id');
})
->selectRaw('table_a.*,sum( inventory.quantity ) in_quantity,sum( inventory.quantity_lock ) in_quantity_lock ')
->groupBy('table_a.item_id');
$result=DB::table('erp_inventory as inven')
->rightJoinSub($temp_b,'table_b',function(QBuilder $query){
$query->on('inven.product_id','=','table_b.product_id');
})
->selectRaw('table_b.*,sum( CASE inven.warehouse_id WHEN 1 THEN inven.quantity ELSE 0 END ) dg_quantity')
->groupBy('table_b.item_id')
->orderByDesc('self')
->get();
PREPARE SELECT
table_b.*,
SUM( CASE inven.warehouse_id WHEN 1 THEN inven.quantity ELSE 0 END ) dg_quantity
FROM
`erp_inventory` AS `inven`
RIGHT JOIN (
SELECT
table_a.*,
SUM( inventory.quantity ) in_quantity,
SUM( inventory.quantity_lock ) in_quantity_lock
FROM
`erp_inventory` AS `inventory`
RIGHT JOIN (
SELECT
orders.order_code,
items.quantity AS item_quantity,
items.product_id,
items.id AS item_id,
product.sku,
product.cn_name,
SUM( CASE cate.category_id WHEN 26 THEN 1 ELSE 0 END ) self
FROM
`erp_orders_detail` AS `items`
LEFT JOIN `erp_orders` AS `orders` ON `items`.`order_id` = `orders`.`id`
LEFT JOIN `erp_product` AS `product` ON `product`.`id` = `items`.`product_id`
LEFT JOIN `erp_product_category` AS `cate` ON `cate`.`product_id` = `items`.`product_id`
WHERE
`orders`.`status` = ?
GROUP BY
`items`.`id`
) AS `table_a` ON `inventory`.`product_id` = `table_a`.`product_id`
GROUP BY
`table_a`.`item_id`
) AS `table_b` ON `inven`.`product_id` = `table_b`.`product_id`
GROUP BY
`table_b`.`item_id`
ORDER BY
`self` DESC
比起这个玩法要花俏一些了:$result=DB::select(“原始SQL”)