开发者

Insert data from other table and update on duplicate key

开发者 https://www.devze.com 2023-04-09 16:39 出处:网络
I have this query where I want to insert into table exp_channel_data data from the table homes but it inserts 7 and 8 entries and then returns the error:

I have this query where I want to insert into table exp_channel_data data from the table homes but it inserts 7 and 8 entries and then returns the error:

[Err] 1062 - Duplicate entry '8' for key 'PRIMARY'

This is my query (I reduced the fields to be readable):

INSERT I开发者_Python百科NTO exp_channel_data (entry_id,site_id,channel_id,field_id_1)  SELECT 7,1,1,summary FROM homes ON DUPLICATE KEY UPDATE entry_id = entry_id+1


If you want to copy all your homes table data into the new one:

INSERT INTO exp_channel_data 
    (entry_id, site_id, channel_id, field_id_1) 

  SELECT homes_id + (COALESCE(maxExp,0) + 1 - minHomes)
       , site_id, channel_id, summary 
  FROM homes AS h
    CROSS JOIN
      ( SELECT MAX(entry_id) AS maxExp
        FROM exp_channel_data
      ) AS mh
    CROSS JOIN
      ( SELECT MIN(homes_id) AS minHomes
        FROM exp_channel_data
      ) AS me
0

精彩评论

暂无评论...
验证码 换一张
取 消