简体   繁体   中英

How to count total number of record for each 7 days in mysql

i trying to get data from database with counting the total number start from today and auto reduce 1 days and count for 7 time. It will display like

Monday    = 5
Tuesday   = 2
Wednesday = 6
Thursday  = 4
Friday    = 7
Saturday  = 1
Sunday    = 5

The problems is when my using the query below

SELECT count(*), ad_date 
  FROM advertise
 WHERE ad_date >=ad_date(day,datediff(day,0,GetDate())- 7,0)

It showing error:

Incorrect parameter count in the call to native function 'datediff'

So what is the problems for this or any alternative query for my problems?

The format of my date table is using timestamp.

2016-03-23 14:27:28 
2016-03-25 18:27:28 
2016-03-26 18:27:28`

datediff has only two parameters

DATEDIFF(date1,date2)

  1. startdate where you want to starts from
  2. EndDate

Replace:

datediff(day,0,GetDate())- 7,0)

With:

DATE(DATE_SUB(NOW(),INTERVAL 7 DAY),GetDate())

Updated Answer (Try This):

SELECT COUNT(1)
FROM table_name
WHERE created_date > (NOW() - INTERVAL 7 DAY)
Group by created_date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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