简体   繁体   中英

MYSQL copy and increase existing rows

Hi I have an table allNews that has 150 rows set up as follows newsID = autoInc:

newsID    newsIDInt    siteID    catID    title
31        1            3         4        Item 1 
32        2            3         4        Item 2

uoto
180       150          3         4        Item 150

I need to duplicate into the same table so the result would be

newsID    newsIDInt    siteID    catID    title
181       1            3         5        Item 1 
182       2            3         5        Item 2

uoto
230       150          3         5        Item 150

ie duplicate and change field catID to 5.

Is this possible using a mysql script?

MrWarby

You can insert into the table and select the values you need (adjusting as neccessary):

INSERT INTO allNews 
        SELECT newsId + 150, newsIDInt, siteID,catID+1,title 
        FROM allNews
        WHERE newsId BETWEEN 31 AND 180;

(Though you may want to have an auto increment take care of the newsId field).

You can avoid to specify the newsID (if it is autoincrement) and force the catID to 5:

    insert into allNews (newsIDInt, siteID, catID, title)
    select newsIDInt, siteID, 5, title from allNews

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