To find missing groups in the table _tgroup I use the syntax WHERE NOT EXISTS by crossing the values of the _tgroup columns of the two tables ta_group and tb_group.
SELECT
`_tgroup`
FROM
`ta_group` q
WHERE
NOT EXISTS ( SELECT 1 FROM `tb_group` t WHERE q.`_tgroup` = t.`_tgroup` );
The values of columns _tdate and _tnumber of the table tb_group are obtained from a table where accesses are recorded for individual groups, therefore in column _tdate you find the access date, in column _tnumber you find the number of accesses that occurred with that group… here you find the simulation
I have to insert into the tb_group table all the groups that are missing in the tb_group table, taking them from the ta_group table, for each day recorded in the tb_group table
INSERT INTO tb_group
SELECT tb._tdate,ta._tgroup,0
FROM ((SELECT DISTINCT _tdate FROM tb_group) tb
LEFT JOIN ta_group ta
ON 1 = 1
)
ON DUPLICATE KEY
UPDATE _tnumber = _tnumber;
(strictly speaking i’m not sure the outer parentheses or the LEFT designator are technically needed, but… it works.)