Mysql date query data packet without filling 0

Foreword

This article is not good to take the title. . . (Mainly the continuous generation of date), we have the key point: Mysql acquisition date list of all the specified time period,
Mysql query grouping by date date no data also check out together.

Original article link address: http: //nullpointer.pw/Mysql%E6%97%A5%E6%9C%9F%E5%88%86%E7%BB%84%E6%97%A0%E6%95%B0 % E6% 8D% AE% E6% 9F% A5% E8% AF% A2% E5% A1% AB% E5% 85% 850.html

problem

Products made a demand, need to show such a line chart to reflect the increasing trend specified period of time users of the site, so the need for back-end engineers to give the corresponding JSON interface.

careless

Specific table structure and data is such

JSON engineer without thinking, launched a CRUD Dafa, smoothly wrote a SQL, less than 5 minutes to complete the interface live, did not test measured directly to the front-end development.

select date(t.create_time) as `date`,
       count(t.id)  as num
from t_user t
group by `date`;

Get the distal begin drawing data, completely wrong results are plotted in FIG ah, because the time is not continuous. So back to the JSON engineers here.

JSON engineer I thought, Oh, did not consider the day off without data, grouping query, then certainly the lack of data on this day.

+------------+-----+
|    date    | num |
+------------+-----+
| 2019-05-06 |  2  |
+------------+-----+
| 2019-05-08 |  2  |
+------------+-----+
| 2019-05-09 |  9  |
+------------+-----+

The correct way even if no data is day, 0 is also filled with a recorded value.

Query data correction

Found the problem can be easily handled, stroked a little logic into a two-step

First, get all dates

Updated September 12, 2019

Thanks to the comments section of a small cute @ (feeling is an interesting man ha ha) the proposed method, so I look to update the article. The best way is to generate a continuous process Java code date, and creates a map object, initialize all the key value of 0, then the query to traverse out according to date as a key, then be put to override the default values .

A generation date continuous process

public static Set<String> getBetweenDate(String start, String end) {
  LocalDate startDate = LocalDate.parse(start);
  LocalDate endDate = LocalDate.parse(end);
  long between = ChronoUnit.DAYS.between(startDate, endDate);
  if (between < 1) {
    return Stream.of(start, end).collect(Collectors.toSet());
  }

  return Stream.iterate(startDate, e -> e.plusDays(1))
    .limit(between + 1)
    .map(LocalDate::toString)
    .collect(Collectors.toSet());
}


public static void main(String[] args) {
  Set<String> days = BaseTest.getBetweenDate("2019-08-29", "2019-09-02");
  log.info("{}", days);
}

Second, grouped by date query

The original article may be misleading, updated off
group by day inquiry, traverse the query results, the first step in to cover the value of the time map.

结果如下:

+------------+-----+
|    date    | num |
+------------+-----+
| 2019-05-03 |  0  |
+------------+-----+
| 2019-05-04 |  0  |
+------------+-----+
| 2019-05-05 |  0  |
+------------+-----+
| 2019-05-06 |  2  |
+------------+-----+
| 2019-05-07 |  0  |
+------------+-----+
| 2019-05-08 |  2  |
+------------+-----+
| 2019-05-09 |  9  |
+------------+-----+

顺利达到了目的,收工!

参考

  • https://www.cnblogs.com/zhengguangpan/p/10308886.html

  • https://blog.csdn.net/Dai_Aixy/article/details/83144619

  • https://www.cnblogs.com/dennyzhangdd/p/8073181.html

Guess you like

Origin www.cnblogs.com/vcmq/p/12149646.html