Please explain how this query generates a calendar table in MySQL This page has a pretty neat way of generating a robust calendar table. I've copy-pasted it below:

CREATE TABLE calendar_table (
    dt DATE NOT NULL PRIMARY KEY,
    y SMALLINT NULL,
    q tinyint NULL,
    m tinyint NULL,
    d tinyint NULL,
    dw tinyint NULL,
    monthName VARCHAR(9) NULL,
    dayName VARCHAR(9) NULL,
    w tinyint NULL,
    isWeekday BINARY(1) NULL,
    isHoliday BINARY(1) NULL,
    holidayDescr VARCHAR(32) NULL,
    isPayday BINARY(1) NULL
);

CREATE TABLE ints ( i tinyint );

INSERT INTO ints VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

INSERT INTO calendar_table (dt)
SELECT DATE('2010-01-01') + INTERVAL a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i DAY
FROM ints a JOIN ints b JOIN ints c JOIN ints d JOIN ints e
WHERE (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) &lt;= 11322
ORDER BY 1;


I don't understand what is happening in the last query. Can someone explain it to me step by step?
I don't know what you want in the explanation, but the last part of the query does a cross join (Cartesian join) to generate an ordered sequence of numbers from 0 to 11322 using the digits table named ints. The query then adds these number to date '2010-01-01' as day interval, thus generating

'2010-01-01'
'2010-01-02'
'2010-01-03'
.....
.....
'2040-12-31'


etc. till you add 11322 days, that is '2040-12-31'. These dates are then inserted into the dt field of the calendar_table.

Well this is a common way of creating a date dimension table. Once the dates are inserted you can update rest of the fields. You generally want to create your organization's holiday table and a payday logic or payday table to update the holiday and payday fields. Rest of the fields can be updated with a query with date functions as below.

update calendar_table
set y = year(dt),
q = quarter(dt),
m = month(dt),
d = day(dt),
dw = dayofweek(dt),
monthName = monthname(dt),
dayName = dayname(dt),
w = week(dt),
isWeekday=
case  
   when dayofweek(dt) = 1 or dayofweek(dt) = 7 then 0
   else 1
end;


Cheers.