简体   繁体   English

如何在 mysql 中为每一天(分组)日期

[英]How to (group by) date for every day in mysql

For my site admin panel, i need to show statistics for payments between two date对于我的网站管理面板,我需要显示两个日期之间的付款统计信息

My payments table fields:我的付款表字段:

+--------------------+--------------+------+-----+-------------------+-----------------------------+
| Field              | Type         | Null | Key | Default           | Extra                       |
+--------------------+--------------+------+-----+-------------------+-----------------------------+
| id                 | int(11)      | NO   | PRI | NULL              | auto_increment              |
| price              | int(11)      | NO   |     | 0                 |                             |
| created            | timestamp    | NO   |     | CURRENT_TIMESTAMP |                             |
+--------------------+--------------+------+-----+-------------------+-----------------------------+

I need to get the payments data between tow days, day by day by php laravel to this structure我需要每天通过 php laravel 获取两天之间的付款数据到这个结构

// date between 2021-12-01 and 2021-12-03

$data = [
    [
        'date' => '2021-12-01',
        'count' => 5 // number of payments records in 2021-12-01 in payments table
    ],
    [
        'date' => '2021-12-02',
        'count' => 0 // number of payments records in 2021-12-01 in payments table
    ],
    [
        'date' => '2021-12-03',
        'count' => 15 // number of payments records in 2021-12-01 in payments table
    ],
];

You should know maybe a day i haven't any payment and in database this day hasen't any records.您应该知道,也许有一天我没有任何付款,并且这一天在数据库中没有任何记录。 but i need to show this day with count of 0但我需要以 0 的数量显示这一天

I don't know how do this我不知道怎么做

Using a recursive common table expression (in mysql 8 or mariadb 10.2+) to create your table of dates (here, reporting dates from 2021-01-01 to 2021-01-31):使用递归公用表表达式(在 mysql 8 或 mariadb 10.2+ 中)创建日期表(此处报告日期从 2021-01-01 到 2021-01-31):

with recursive dates as (
    select date('2021-01-01') date
    union all
    select dates.date + interval 1 day from dates where dates.date < '2021-01-31'
)
select dates.date, count(payments.id)
from dates
left join payments on date(payments.created)=dates.date
group by 1;

You can do that using this query:您可以使用以下查询来做到这一点:


select gen_date, count(transactions.id) from

(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from
 (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
 left JOIN  transactions
  on date(transactions.created) = date(gen_date)

where gen_date between '2021-12-01' and '2021-12-20'
group by gen_date;

if you want laravel query builder version:如果你想要 laravel 查询生成器版本:

    $select = DB::query()->selectRaw("gen_date, count(transactions.id)")->fromSub(function ($query) {
        $query->select("(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from
 (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4)");
    }, "v")
        ->leftJoin('transactions', function ($join) {
            $join->on(DB::raw("date(transactions.created)"), "=", DB::raw("date(v.gen_date)"));
        })
        ->whereRaw("gen_date between '2021-12-01' and '2021-12-20'")
        ->groupBy('gen_date')
        ->get();

ps: the date range subquery I taken from here ps:我从这里获取的日期范围子查询

You can use DB::raw() :您可以使用DB::raw()

$data = DB::raw('select left(created_at, 10) as date, count(1) as count from payments group by date;');

this code can show you the days of between two days此代码可以显示两天之间的天数

select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2021-12-01' and '2021-12-03'

i hobe this code can help you我希望这段代码可以帮助你

SELECT DATE(created) AS `date`, COUNT(*) AS `count`
FROM source_table
-- WHERE created BETWEEN @period_start AND @period_finish
GROUP BY `date`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM