如何从多个表中查找一个月内的日常收藏

时间:2018-07-10 03:34:57

标签: 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 

我试图从两个以上的表格中查找一个月内的每日总收藏。 在这里,可以按照以下方式计算每个凭证集合

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

以此类推。

它没有给出正确的输出。但我需要以这种方式解决。谢谢

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

2 个答案:

答案 0 :(得分:1)

您还需要检查年份以获取正确的信息,否则也会从其他年份获取结果。

$ itemsPrice将为SELECT SUM(purchase_price * quantity) AS price FROM carts WHERE carts.invoice_id = invoices.id

我们可以获得以下结果:

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

但是我们可以改善查询,我们不应该在查询中两次获取$ itemsPrice,因此可以做一些数学运算:

如果x = itemsPricey = discountz = vat,则

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

由于x-y+((xz)/100)等于(x(100+z) - 100y)/100,因此查询将是。

$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
                    ) *(100 + vat) - 100 * discount
                ) / 100
            ) AS sum"))
         ->whereMonth('updated_at', '2')
         ->whereYear('updated_at', '2018')
         ->groupBy('date')
         ->get();

PS :我尚未对其进行测试,因此如果出现问题,请及时更新。

答案 1 :(得分:0)

您可以尝试此方法,尽管尚未测试

子查询(按发票计算价格组)

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

阅读

foreach($reports as $report){
    echo $report->today ." " . $report->sum;
}