Try this:
SELECT
DATE(submitdate) 'Day',
SUM(clicks)
FROM Table
WHERE DATEDIFF(NOW(), submitdate) <= 31
GROUP BY DATE(submitdate);
Update: For this you will need to generate a list of the last n days from the current date NOW()
. I used for this an anchor table:
CREATE TABLE TEMP
(Digit int);
INSERT INTO Temp VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
Then I used it to generate a list of the last n days from now(in the demo I used 31 days but you can try it with any value less than 100, if you want to get more than 100 you will need to CROSS JOIN temp
table one more time) I used the following query:
SELECT
DATE_SUB(NOW(), INTERVAL id DAY) GDay
FROM
(
SELECT t2.digit * 10 + t1.digit + 1 AS id
FROM TEMP AS t1
CROSS JOIN TEMP AS t2
) t WHERE id <= 31;
Then with LEFT JOIN
you can get the last 31 days and if there are no records there will be 0 instead:
SELECT
DATE(t2.GDay) 'Day',
SUM(IFNULL(t1.clicks, 0)) 'Count of clicks'
FROM
(
SELECT
DATE_SUB(NOW(), INTERVAL id DAY) GDay
FROM
(
SELECT t2.digit * 10 + t1.digit + 1 AS id
FROM TEMP AS t1
CROSS JOIN TEMP AS t2
) t WHERE id <= 31
) t2
LEFT JOIN Table1 t1 ON DATE(t1.submitdate) = DATE(t2.GDay)
GROUP BY DATE(t2.GDay)
ORDER BY DAY DESC;