user3193843
user3193843

Reputation: 305

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

Upvotes: 0

Views: 31

Answers (2)

Jim
Jim

Reputation: 22656

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

Upvotes: 2

winterlude
winterlude

Reputation: 131

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

Upvotes: 2

Related Questions