Get the App
SLTechnology News&Howtos  ›  Database  › 

Use SQL to count the daily total of a table and the incremental mysql database

Shulou Source: shulou.com Published: 2022-06-01 03:29:05 10月27日 Update

For example: a user table has a user_id self-incrementing primary key and a create_time user creation time

Business requirements allow you to count the total number of users per day and how many users have increased compared to the previous day

As shown in figure 2.

First of all, check out the total amount of each day. The time in the table is generally of date_time type, so use a function.

SELECT DATE_FORMAT (create_time,'%Y-%m-%d') dd,COUNT (*) num FROM USER GROUP BY DATE_FORMAT (create_time,'%y-%m-%d')

Use the previous SQL as a statistical table to calculate the incremental SQL. You can try it for yourself.

SELECT a.dd.nummam (lt.num) AS cum FROM (SELECT DATE_FORMAT (create_time,'%Y-%m-%d') dd,COUNT (*) num FROM USER GROUP BY DATE_FORMAT (create_time,'%y-%m-%d')) aJOIN (SELECT DATE_FORMAT (create_time,'%Y-%m-%d') dd,COUNT (*) num FROM USER GROUP BY DATE_FORMAT (create_time) '% ymi% mmi% d') ltON a.dd > = lt.dd GROUP BY dd

(table 1) (Table 2)

Tags: Users totals statistics time increments businesses examples functions charts users types statistics requirements data databases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Linux NVidia MySQL