简体   繁体   中英

Database: Importing data from other tables to another tables

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_INCREMENT
  • cf_cal_events.model_id is the PRIMARY KEY and does NOT have AUTO_INCREMENT
  • cf_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:

  1. 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.
    • the relation between the source and the destination is through the cf_cal_events.col_10 column.
  2. UPDATE events changes in the source database.

  3. 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:

  1. 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

  2. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM