根据Laravel中日期的月份获取行数 [英] get row count according to the month in the date in Laravel

查看:317
本文介绍了根据Laravel中日期的月份获取行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望laravel中的查询将dateofSale月份的数据与同一月份的行数进行分组.

I want a query in laravel to group by data with dateofSale's month with the count of rows with the same month.

这是我的桌子

我想输出类似这样的内容

and I want to output something like this

numberOfSales | Month
     2           Nov
     1           Oct

我该怎么办?

推荐答案

您应该能够使用以下方法实现此目标:

You should be able to achieve this what you're after with:

$data = \App\SaleData::selectRaw('COUNT(*) as count, YEAR(dateOfSale) year, MONTH(dateofSale) month')
    ->groupBy('year', 'month')
    ->get();

如果您还不包括年份,则同一个月内将包含不同年份的销售额,例如2017年10月和2018年10月的销售将一并包括在内.

If you don't include the year as well then you'll have sales from different years included in the same month e.g. sales from October 2017 and October 2018 will be included together.

这篇关于根据Laravel中日期的月份获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆