Dengan asumsi postid
. Anda s bersebelahan di tabel Anda, maka kueri ini:
SELECT
DATE_FORMAT(b.date, '%Y-%m-%d') date,
COUNT(c.postid)
FROM
(
SELECT
(SELECT MAX(date) FROM ex) + INTERVAL 3 DAY - INTERVAL a.postid DAY AS date
FROM
ex a
) b
LEFT JOIN
ex c ON c.date = b.date
GROUP BY
b.date
ORDER BY
b.date
menghasilkan:
date COUNT(c.postid)
2012-01-01 2
2012-01-02 2
2012-01-03 0
2012-01-04 2
2012-01-05 1
2012-01-06 0
2012-01-07 0
Lihat http://sqlfiddle.com/#!2/2cf8d/2
Jika postid
. Anda s tidak bersebelahan, maka Anda dapat menggunakan ids
tabel id yang berdekatan:
SELECT
DATE_FORMAT(b.date, '%Y-%m-%d') date,
COUNT(c.postid)
FROM
(
SELECT
(SELECT MAX(date) FROM ex) + INTERVAL 3 DAY - INTERVAL a.id DAY AS date
FROM
ids a
) b
LEFT JOIN
ex c ON c.date = b.date
GROUP BY
b.date
ORDER BY
b.date DESC
LIMIT 7
Lihat http://sqlfiddle.com/#!2/13035/1