How does php mysql get the number of orders per day, 7 days a week?

question 1:
1, the array of $array is as follows:

array (size=7)
  1 => string "2018-09-21" (length=10)
  2 => string "2018-09-22" (length=10)
  3 => string "2018-09-23" (length=10)
  4 => string "2018-09-24" (length=10)
  5 => string "2018-09-25" (length=10)
  6 => string "2018-09-26" (length=10)
  7 => string "2018-09-27" (length=10)
The array of

2 and $list is as follows:

array (size=3)
  0 => 
    array (size=2)
      "riqi" => string "2018-09-21" (length=10)
      "count" => int 2
  1 => 
    array (size=2)
      "riqi" => string "2018-09-26" (length=10)
      "count" => int 2
  2 => 
    array (size=2)
      "riqi" => string "2018-09-27" (length=10)
      "count" => int 3

ask for help: how to php achieve the following effects

array (size=3)
  0 => 
    array (size=2)
      "riqi" => string "2018-09-21" (length=10)
      "count" => int 2
  1 => 
    array (size=2)
      "riqi" => string "2018-09-22" (length=10)
      "count" => int 0
  2 => 
    array (size=2)
      "riqi" => string "2018-09-23" (length=10)
      "count" => int 0
  3 => 
    array (size=2)
      "riqi" => string "2018-09-24" (length=10)
      "count" => int 0
  4 => 
    array (size=2)
      "riqi" => string "2018-09-25" (length=10)
      "count" => int 0
  5 => 
    array (size=2)
      "riqi" => string "2018-09-26" (length=10)
      "count" => int 2
  6 => 
    array (size=2)
      "riqi" => string "2018-09-27" (length=10)
      "count" => int 3
< hr >

question 2:
the existing sql is as follows:

select FROM_UNIXTIME(addtime,"%Y-%m-%d") as riqi, count(1) as count from order where FROM_UNIXTIME(addtime,"%Y-%m-%d") >= now() - interval 7 day group by FROM_UNIXTIME(addtime,"%Y-%m-%d")

how to achieve the following effects with one sentence of sql

array (size=3)
  0 => 
    array (size=2)
      "riqi" => string "2018-09-21" (length=10)
      "count" => int 2
  1 => 
    array (size=2)
      "riqi" => string "2018-09-22" (length=10)
      "count" => int 0
  2 => 
    array (size=2)
      "riqi" => string "2018-09-23" (length=10)
      "count" => int 0
  3 => 
    array (size=2)
      "riqi" => string "2018-09-24" (length=10)
      "count" => int 0
  4 => 
    array (size=2)
      "riqi" => string "2018-09-25" (length=10)
      "count" => int 0
  5 => 
    array (size=2)
      "riqi" => string "2018-09-26" (length=10)
      "count" => int 2
  6 => 
    array (size=2)
      "riqi" => string "2018-09-27" (length=10)
      "count" => int 3
Jul.20,2021

purpose: to realize the assembly of data; 1. Loop the date array 2. Within each loop code, call the function or method 3. The above function or method is to query the data according to the date and return the result of the number of orders


SELECT FROM_UNIXTIME (addtime,'%Y-%m-%d') as riqi,count (*) from order GROUP BY riqi ORDER BY riqi LIMIT 7
should be the result you want


use to_days (now ())-to_days (addtime) < = 7 to retrieve the data of the last 7 days, and then use the date method to retain the time only to the number of days. Then according to the data found, grouped according to the time, and then count (order), we get the corresponding number of orders per day.
if you want to show the number of days in which the order number is 0, write a date cycle to determine whether the date of each day has a corresponding record in the query result. If there is no, it is 0.
effect:


it is better to store a timestamp in the database

.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e50e34-27627.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e50e34-27627.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?