如何从多个表中查找一个月内的日常集合

i1icjdpr  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(428)
  1. Carts Table structure:
  2. id , invoice_id, product_id, quantity , purchase_price, created_at, updated_at
  3. Invoices Table structure:
  4. id, customer_id, discount, vat, created_at, updated_at

我试着从上面两张表中找出一个月内每天的总收藏量。在这里,每一张代金券的收款可以按以下方式计算

  1. static function totalPrice($id)
  2. {
  3. $itemsPrice = 0;
  4. $prices = DB::table('invoices')
  5. ->join('carts','carts.invoice_id','invoices.id')
  6. ->select('purchase_price','quantity')
  7. ->where('carts.invoice_id','=',$id)
  8. ->get();
  9. $invoice_price = DB::table('invoices')
  10. ->select('vat','discount')
  11. ->where('invoices.id','=',$id)
  12. ->first();
  13. foreach ($prices as $price)
  14. {
  15. $itemsPrice = $itemsPrice+ $price->purchase_price*$price->quantity;
  16. }
  17. $itemsPrice = $itemsPrice - $invoice_price->discount + (($itemsPrice*$invoice_price->vat)/100);
  18. return $itemsPrice;
  19. }

但问题是我想通过查询生成器进行计算。
预期输出如下:

  1. Today Sum
  2. 01-01-2018 1200
  3. 02-01-2010 1100
  4. 03-01-2018 1200
  5. 04-01-2018 1030

以此类推。。
它不能提供正确的输出。但我需要用这种方法来解决。谢谢

  1. $monthly_report_chart = DB::table("invoices")
  2. ->select( DB::raw("DATE_FORMAT(updated_at, '%d/%m/%Y ') as today"),
  3. DB::raw("SUM(collected_today) as sum")
  4. )
  5. ->groupBy(DB::raw('Date(updated_at)'))
  6. ->whereMonth('updated_at','=',date('m'))
  7. ->get();
fslejnso

fslejnso1#

你可以试试这个,虽然没有测试过
子查询(按发票计算价格组)

  1. $sub = DB::table("invoices")
  2. ->join('carts','carts.invoice_id','invoices.id')
  3. ->select('vat', 'discount', DB::raw("SUM(purchase_price * quantity) as totalPrice"), 'updated_at')
  4. ->groupBy('invoices.id')
  5. ->whereMonth('updated_at','=',date('m'));

使用子查询计算报表

  1. $reports = DB::table( DB::raw("({$sub->toSql()}) as sub") )
  2. ->select(
  3. DB::raw("DATE_FORMAT(updated_at, '%d/%m/%Y ') as today"),
  4. DB::raw('($totalPrice - discount + (($totalPrice * vat)/100)) as sum')
  5. )
  6. ->mergeBindings( $sub->getQuery() );
  7. ->groupBy(DB::raw('Date(updated_at)'))
  8. ->get();

读一下

  1. foreach($reports as $report){
  2. echo $report->today ." " . $report->sum;
  3. }
展开查看全部
arknldoa

arknldoa2#

您还需要检查年份以获得正确的结果,否则它也会从其他年份获取结果。
$itemsprice将是 SELECT SUM(purchase_price * quantity) AS price FROM carts WHERE carts.invoice_id = invoices.id 我们可以通过以下方法得到这个结果:

  1. $monthly_report_chart = DB::table("invoices")
  2. ->select( DB::raw("
  3. DATE_FORMAT(updated_at, '%d/%m/%Y ') AS date,
  4. SUM(
  5. (
  6. SELECT
  7. SUM(purchase_price * quantity) AS price
  8. FROM
  9. carts
  10. WHERE
  11. carts.invoice_id = invoices.id
  12. )
  13. - discount
  14. + (
  15. (
  16. SELECT
  17. SUM(purchase_price * quantity) AS price
  18. FROM
  19. carts
  20. WHERE
  21. carts.invoice_id = invoices.id
  22. ) * vat / 100
  23. )
  24. ) AS sum"))
  25. ->whereMonth('updated_at', '2')
  26. ->whereYear('updated_at', '2018')
  27. ->groupBy('date')
  28. ->get();

但是我们可以改进查询,我们不应该在查询中两次获取$itemsprice,所以让我们做一些计算:
如果 x = itemsPrice , y = discount 以及 z = vat 然后

  1. Formula x - y+((x * z)/100)
  2. is equal to ((x - y)100 + (x * z))/100
  3. is equal to (100x - 100y + x*z)/100
  4. is equal to (x(100+z) - 100y)/100

x-y+((xz)/100) 等于 (x(100+z) - 100y)/100 然后查询将被删除。

  1. $monthly_report_chart = DB::table("invoices")
  2. ->select( DB::raw("
  3. DATE_FORMAT(updated_at, '%d/%m/%Y ') AS date,
  4. SUM(
  5. (
  6. (
  7. SELECT
  8. SUM(purchase_price * quantity) AS price
  9. FROM
  10. carts
  11. WHERE
  12. carts.invoice_id = invoices.id
  13. ) *(100 + vat) - 100 * discount
  14. ) / 100
  15. ) AS sum"))
  16. ->whereMonth('updated_at', '2')
  17. ->whereYear('updated_at', '2018')
  18. ->groupBy('date')
  19. ->get();

ps:我还没有测试过,所以如果有什么问题请告诉我。

展开查看全部

相关问题