[英]How to change SQL statement to laravel query
我有我的 sql 查詢,我想更改為 laravel 查詢
這是 sql
SELECT
products.id,
products.name,
(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and DATE(stocks.created_at)
= CURDATE()) as stock_in_today,
(select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and
DATE(loadings.created_at) = CURDATE()) as total_loadings_today,
(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id) as total_stock_till_date,
(select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id) as
total_loadings_till_date,
((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and
DATE(stocks.created_at) < CURDATE())-(select ifnull(sum(loadings.qty),0) from loadings where
loadings.pid=products.id and DATE(loadings.created_at) < CURDATE())) as opening_balance,
((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id)-(select
ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id)) as closing_balance
from products
有人可以幫忙
您可以簡單地使用DB
外觀:
DB::select(DB::raw("SELECT
products.id,
products.name,
(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and DATE(stocks.created_at)
= CURDATE()) as stock_in_today,
(select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and
DATE(loadings.created_at) = CURDATE()) as total_loadings_today,
(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id) as total_stock_till_date,
(select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id) as
total_loadings_till_date,
((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and
DATE(stocks.created_at) < CURDATE())-(select ifnull(sum(loadings.qty),0) from loadings where
loadings.pid=products.id and DATE(loadings.created_at) < CURDATE())) as opening_balance,
((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id)-(select
ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id)) as closing_balance
from products"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.