Php 如何从多个表中查找一个月内的日常集合
我试图从上面两张表中找出一个月内每天的总收藏量。 在这里,每个凭证集合可以按以下方式计算Php 如何从多个表中查找一个月内的日常集合,php,mysql,laravel,query-builder,Php,Mysql,Laravel,Query Builder,我试图从上面两张表中找出一个月内每天的总收藏量。 在这里,每个凭证集合可以按以下方式计算 Carts Table structure: id , invoice_id, product_id, quantity , purchase_price, created_at, updated_at Invoices Table structure: id, customer_id, discount, vat, created_at, updated_at 但问题是我想通过查询生成器进行计算
Carts Table structure:
id , invoice_id, product_id, quantity , purchase_price, created_at, updated_at
Invoices Table structure:
id, customer_id, discount, vat, created_at, updated_at
但问题是我想通过查询生成器进行计算
预期输出如下所示:
static function totalPrice($id)
{
$itemsPrice = 0;
$prices = DB::table('invoices')
->join('carts','carts.invoice_id','invoices.id')
->select('purchase_price','quantity')
->where('carts.invoice_id','=',$id)
->get();
$invoice_price = DB::table('invoices')
->select('vat','discount')
->where('invoices.id','=',$id)
->first();
foreach ($prices as $price)
{
$itemsPrice = $itemsPrice+ $price->purchase_price*$price->quantity;
}
$itemsPrice = $itemsPrice - $invoice_price->discount + (($itemsPrice*$invoice_price->vat)/100);
return $itemsPrice;
}
等等
它没有给出正确的输出。但我需要用这种方法来解决它。谢谢
Today Sum
01-01-2018 1200
02-01-2010 1100
03-01-2018 1200
04-01-2018 1030
您还需要检查年份以获得正确的结果,否则它也将从其他年份获取结果 $itemsPrice is将
选择SUM(采购价格*数量)作为carts的价格,其中carts.invoice\u id=invoices.id
我们可以通过以下方式获得此结果:
$monthly_report_chart = DB::table("invoices")
->select( DB::raw("DATE_FORMAT(updated_at, '%d/%m/%Y ') as today"),
DB::raw("SUM(collected_today) as sum")
)
->groupBy(DB::raw('Date(updated_at)'))
->whereMonth('updated_at','=',date('m'))
->get();
但是我们可以改进查询,我们不应该在查询中获取$itemsPrice两次,所以让我们做一些数学计算:
如果x=itemsPrice
,y=折扣
和z=vat
,则
$monthly_report_chart = DB::table("invoices")
->select( DB::raw("
DATE_FORMAT(updated_at, '%d/%m/%Y ') AS date,
SUM(
(
SELECT
SUM(purchase_price * quantity) AS price
FROM
carts
WHERE
carts.invoice_id = invoices.id
)
- discount
+ (
(
SELECT
SUM(purchase_price * quantity) AS price
FROM
carts
WHERE
carts.invoice_id = invoices.id
) * vat / 100
)
) AS sum"))
->whereMonth('updated_at', '2')
->whereYear('updated_at', '2018')
->groupBy('date')
->get();
由于x-y+((xz)/100)
等于(x(100+z)-100y)/100
,因此查询将被删除
Formula x - y+((x * z)/100)
is equal to ((x - y)100 + (x * z))/100
is equal to (100x - 100y + x*z)/100
is equal to (x(100+z) - 100y)/100
PS:我还没有测试过它,所以如果出现问题,请更新我。您可以试试这个,尽管没有测试过 子查询(按发票计算价格组) 使用子查询计算报表
$sub = DB::table("invoices")
->join('carts','carts.invoice_id','invoices.id')
->select('vat', 'discount', DB::raw("SUM(purchase_price * quantity) as totalPrice"), 'updated_at')
->groupBy('invoices.id')
->whereMonth('updated_at','=',date('m'));
读一下
$reports = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->select(
DB::raw("DATE_FORMAT(updated_at, '%d/%m/%Y ') as today"),
DB::raw('($totalPrice - discount + (($totalPrice * vat)/100)) as sum')
)
->mergeBindings( $sub->getQuery() );
->groupBy(DB::raw('Date(updated_at)'))
->get();
您是否对每张发票有不同的增值税?是的,如果您将总金额存储在发票表中,然后使用更简单的查询(按日期分组)就可以了;)我知道这很容易,但问题是数量更新太频繁,很难维护总量。carts表的任务是加入查询检查再次检查我在
选择中这样做增值税是%,应与总价-折扣(而不是百分比)一起添加。这不是你的公式吗<代码>商品折扣-折扣+((商品折扣*增值税)/100)
是。当我实现您的查询时,它显示了错误的答案。这就是为什么我要问-100*折扣
不应该偏离100
$reports = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->select(
DB::raw("DATE_FORMAT(updated_at, '%d/%m/%Y ') as today"),
DB::raw('($totalPrice - discount + (($totalPrice * vat)/100)) as sum')
)
->mergeBindings( $sub->getQuery() );
->groupBy(DB::raw('Date(updated_at)'))
->get();
foreach($reports as $report){
echo $report->today ." " . $report->sum;
}