【问题标题】:Get data by quantity and date using Laravel使用 Laravel 按数量和日期获取数据
【发布时间】:2020-07-18 14:42:16
【问题描述】:

我的 mysql 数据库中有一个用户表,用于存储用户创建日期。 我想获取过去 7 天的注册用户数。 示例:

[6,4,8,6,5,6,7]

其中每个数字代表 7 天内每个日期的注册用户数。 我如何使用 Laravel 做到这一点?

【问题讨论】:

  • 按日期分组并获取计数。

标签: mysql laravel


【解决方案1】:

您可以使用以下解决方案,您可以获得普通数组,也可以使用另一个数组,其中键是日期,值是当天注册的用户数。

$usersPerDay = User::select(DB::raw('count(id) as `number_of_users`'),DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') new_date"))
                    ->whereRaw('DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
                    ->groupBy('new_date')->orderBy('new_date')->get();

print_r($usersPerDay->pluck('number_of_users')->toArray());

print_r($usersPerDay->pluck('number_of_users', 'new_date'));

由于您的要求是从日期范围 (link) 生成天数,因此您需要执行以下操作。 首先,更新 config/database.php 文件中的 strict 值

'mysql' => [
    ...
    'strict' => false,
    ...
]

然后运行下面的查询得到期望的结果

$query = "select 
            t1.new_date,
            coalesce(SUM(t1.number_of_users+t2.number_of_users), 0) AS number_of_users
            from
            (
              select DATE_FORMAT(a.Date,'%Y-%m-%d') as new_date,
              '0' as  number_of_users
              from (
                select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
                from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
                cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
                cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
              ) a
              where a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
            )t1
            left join
            (
              SELECT DATE_FORMAT(created_at,'%Y-%m-%d') AS created_at, 
              COUNT(*) AS number_of_users
              FROM users
              WHERE DATE_SUB(created_at, INTERVAL 1 DAY) > DATE_SUB(DATE(NOW()), INTERVAL 1 WEEK) 
              GROUP BY DAY(created_at) DESC
            )t2
            on t2.created_at = t1.new_date
            group by DAY(t1.new_date)
            order by t1.new_date asc";
        $users = DB::select($query);
        $usersPerDay = collect($users)->pluck('number_of_users')->toArray();
        print_r($usersPerDay);
        die;

【讨论】:

  • 您好,我需要在这个 7 个位置的数组中返回数据,就是每天的数量:[6,4,8,6,5,6,7]
  • 当天没有用户注册会怎样?
  • 例子:[0,0,0,0,0,0,5] 在这个例子中,只有5个用户在“X”日注册(那一天是最近7天内)
【解决方案2】:

使用以下代码,您将获得一个名称和创建时间的数组,然后进行计数以获取用户数:

$previous_week = strtotime("-1 week +1 day");
$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);

$users = User::whereBetween('created_at', [$start_week, $end_week])->get(['name','created_at']);

echo count($users);

Insted of echo 随心所欲。 希望这有效

【讨论】:

  • 嗨@Oriol,我需要在这个7个位置的数组中返回数据,只是每天的数量:[6,4,8,6,5,6,7]跨度>
【解决方案3】:

使用 Laravel 集合groupBy() 函数。

$users = Users::whereBetween(now(), now()->subWeek())
             ->groupBy(function ($user) {
                 return $user->created_at->toDateString();
             })
             ->map(function ($group) {
                 return $group->count();
             })
             ->values()
             ->toArray();

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多