एक सरलीकृत उदाहरण तालिका मानते हुए table1
निम्नलिखित सामग्री के साथ
CREATE TABLE table1 (lastUpdated datetime);
INSERT INTO table1 (lastUpdated) VALUES ('2016-03-23');
INSERT INTO table1 (lastUpdated) VALUES ('2016-03-24');
INSERT INTO table1 (lastUpdated) VALUES ('2016-03-30');
INSERT INTO table1 (lastUpdated) VALUES ('2016-03-31');
INSERT INTO table1 (lastUpdated) VALUES ('2016-04-02');
INSERT INTO table1 (lastUpdated) VALUES ('2016-03-31');
फिर निम्न MySQL स्टेटमेंट पिछले 30 दिनों की सभी तारीखों को 0 की गिनती के साथ लौटाएगा, जहां table1
में कोई एंट्री नहीं मिली है। :
SELECT lastUpdated, count(*)-1 FROM (
SELECT date(lastUpdated) as lastUpdated FROM table1 as t1
UNION ALL
SELECT curdate() - interval a day AS lastUpdated FROM (
select 0 as a union select 1 union select 2 union select 3 union
select 4 union select 5 union select 6 union select 7 union
select 8 union select 9 union select 10 union select 11 union
select 12 union select 13 union select 14 union select 15 union
select 16 union select 17 union select 18 union select 19 union
select 20 union select 21 union select 22 union select 23 union
select 24 union select 25 union select 26 union select 27 union
select 28 union select 29
) as t2
) as t3 GROUP BY lastUpdated;