简体   繁体   English

数据库:将数据从其他表导入到另一个表

[英]Database: Importing data from other tables to another tables

So here's my database groupofficecom 这是我的数据库groupofficecom 我的资料库

I'm importing elements from source to cal_events and cf_cal_events . 我正在将元素从source导入到cal_eventscf_cal_events The database has some rules I can't change: 数据库中有一些我无法更改的规则:

  • cal_events.id is the PRIMARY KEY and has AUTO_INCREMENT cal_events.id是主键,具有AUTO_INCREMENT
  • cf_cal_events.model_id is the PRIMARY KEY and does NOT have AUTO_INCREMENT cf_cal_events.model_id是主键,并没有 AUTO_INCREMENT
  • cf_cal_events.model_id = cal_events.id
  • ID_ELEMENT is the PRIMARY KEY of the VIEW source ID_ELEMENT是VIEW source的PRIMARY KEY
  • 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: 我正在尝试执行一个查询,该查询定期将source元素添加到cal_eventscf_cal_events ,因此它需要应用以下内容:

  1. INSERT new events (elements) INTO both tables, considering that: INSERT新事件(要素) INTO两个表,考虑到:

    • cal_events.id is auto_incremented, and cf_cal_events.model_id gets its value from cal_events.id like in the photo above. cal_events.id是auto_incremented,并且cf_cal_events.model_idcal_events.id获取其值, cal_events.id图所示。
    • the relation between the source and the destination is through the cf_cal_events.col_10 column. source和目标之间的关系是通过cf_cal_events.col_10列进行的。
  2. UPDATE events changes in the source database. UPDATE事件在源数据库中发生更改。

  3. DELETE events deleted in the source database (in the source database, there's a parameter tinyint column ASUPPRIMER='1' ) 在源数据库中删除的DELETE事件(在源数据库中,有一个参数tinyint列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). 我可以将新项目插入cal_events ,但不能将项目插入cf_cal_events (结果是所有重复项)。 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. 当我执行查询时,由于该列的AUTO_INCREMENT,它会再次添加具有新id的元素。 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. 问题是我不能使用REPLACE INTOON DUPLICATE KEY UPDATE因为PRIMARY KEY必须相同才能正常工作。

Here's an SQL Fiddle of my database. 这是我数据库的SQL Fiddle 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 第一个INSERT工作正常,但是另一个返回空结果

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. 这是您对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. 您必须根据条件从源连接到cal_events,以及如何迁移数据,以获取先前已插入的cal_events行。 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. 然后,您必须加入到model_id上方的cf_cal_events才能获得这些cal_events,而cf_cal_events中尚无参考。 And at last you have to select the cal_events.id as model_id for cf_cal_events. 最后,您必须选择cal_events.id作为cf_cal_events的model_id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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