Reputation: 183
So here's my database groupofficecom
I'm importing elements from source
to cal_events
and cf_cal_events
. The database has some rules I can't change:
cal_events.id
is the PRIMARY KEY and has AUTO_INCREMENTcf_cal_events.model_id
is the PRIMARY KEY and does NOT have AUTO_INCREMENTcf_cal_events.model_id = cal_events.id
ID_ELEMENT
is the PRIMARY KEY of the VIEW source
cf_cal_events.col_10 = source.ID_ELEMENT
I'm trying to execute a query that adds elements from source
to cal_events
and cf_cal_events
on a regular basis, so it needs to apply the following:
INSERT
new events (elements) INTO
both tables, considering that:
cal_events.id
is auto_incremented, and cf_cal_events.model_id
gets its value from cal_events.id
like in the photo above.source
and the destination is through the cf_cal_events.col_10
column.UPDATE
events changes in the source database.
DELETE
events deleted in the source database (in the source database, there's a parameter tinyint column ASUPPRIMER='1'
)
I'm not asking people to do my work, I'm asking for help with the problems I'm encountering in my code:
I'm able to insert new items to cal_events
, but I'm not able to insert the items to cf_cal_events
(the result is all duplicates). Those are my tries.
WHERE cf_cal_events.model_id = cal_events.id
and
INNER JOIN cf_cal_events
ON cf_cal_events.model_id = cal_events.id
both returned empty results
When I execute my query, it adds elements again but with new id
s because of the AUTO_INCREMENT of the column. The problem is I can't use the REPLACE INTO
or ON DUPLICATE KEY UPDATE
because the PRIMARY KEY has to be the same in order for this to work.
Here's an SQL Fiddle of my database. Thank you everybody!
Here's my insertion code:
INSERT INTO groupofficecom.cal_events (
calendar_id,
user_id,
start_time,
end_time,
name,
description,
location,
ctime,
mtime,
muser_id,
status
)
SELECT '5' AS calendar_id,
'3' AS user_id,
UNIX_TIMESTAMP(source.DATEDEBUT),
UNIX_TIMESTAMP(source.DATEFIN),
CONCAT(source.C219PRNOM,' ',source.C218NOM),
source.TYPEACTIONS,
source.C222LIEU,
UNIX_TIMESTAMP(source.CREATEDATE),
UNIX_TIMESTAMP(source.MODIF_DATE),
'',
'CONFIRMED' AS status
FROM source;
INSERT INTO groupofficecom.cf_cal_events (
model_id,
col_4,
col_5,
col_6,
col_7,
col_8,
col_9,
col_10
)
SELECT groupofficecom.cal_events.id,
source.C221CODECLIENT,
'' AS col_5,
'0' AS col_6,
source.C218NOM,
source.C219PRNOM,
source.TYPEACTIONS,
source.ID_ELEMENT
FROM source, groupofficecom.cal_events
INNER JOIN cf_cal_events
ON groupofficecom.cf_cal_events.model_id = groupofficecom.cal_events.id;
The first INSERT
works fine, but the other is returning empty results
Upvotes: 1
Views: 67
Reputation: 306
SELECT groupofficecom.cal_events.id,
source.C221CODECLIENT,
'' AS col_5,
'0' AS col_6,
source.C218NOM,
source.C219PRNOM,
source.TYPEACTIONS,
source.ID_ELEMENT
FROM source
INNER JOIN groupofficecom.cal_events
ON groupofficecom.cal_events.calendar_id = 5
AND groupofficecom.cal_events.user_id = 3
AND groupofficecom.cal_events.start_time = UNIX_TIMESTAMP(source.DATEDEBUT)
AND groupofficecom.cal_events.end_time = UNIX_TIMESTAMP(source.DATEFIN)
...
LEFT JOIN cf_cal_events
ON groupofficecom.cf_cal_events.model_id = groupofficecom.cal_events.id
WHERE groupofficecom.cf_cal_events.model_id IS NULL
That's your insert query for cf_cal_events. You have to join from source to cal_events with conditions, how you migrated the data, to get the cal_events-rows you had previously insert. Then you have to join to cf_cal_events above the model_id to get these cal_events, which have no reference in cf_cal_events yet. And at last you have to select the cal_events.id as model_id for cf_cal_events.
Upvotes: 1