grafixart Posted May 18, 2009 Share Posted May 18, 2009 I want to insert into a table called hre_cat The already existing fields are: id, parent_id, title, name, alias, image, section, image_position, description, published, checked_out_time, editor, ordering, access, count, params For each record already existing, I want to insert a new record that takes it's id from the id field for each existing record and puts it into the parent_id field. So the id field for each new record has to start at 36703 and go to 73406 (36703+1...73406), the parent_id field has to go from 1-36703, and the section field needs to be equal to the section field of the parent (an integer). How would I phrase this in an insert command? Do I need to use php? Can anyone help? Thanks! Link to comment https://forums.phpfreaks.com/topic/158590-how-to-insert-this/ Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 Wouldn't that cause duplicates for all numbers except 73407? Link to comment https://forums.phpfreaks.com/topic/158590-how-to-insert-this/#findComment-836436 Share on other sites More sharing options...
grafixart Posted May 18, 2009 Author Share Posted May 18, 2009 No, the existing records are only up until 36703 so (36703+1,2,3...73406) would be new for ID and then for the parent_id it would list #s from 1-36703. Do you understand? How would I write this using an insert command? I want to insert multiple records at once using a formula. New to sql. Thanks! Link to comment https://forums.phpfreaks.com/topic/158590-how-to-insert-this/#findComment-836468 Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 Is the id column set to AUTO_INCREMENT? Look up on INSERT...SELECT. I believe that's what you want. Link to comment https://forums.phpfreaks.com/topic/158590-how-to-insert-this/#findComment-836469 Share on other sites More sharing options...
grafixart Posted May 18, 2009 Author Share Posted May 18, 2009 Yes, the id column is set to auto increment. I know how to do a basic insert statement. What I need to understand is how to phrase this in the insert statement: "the parent_id field has to go from 1-36703, and the section field needs to be equal to the section field of the parent (an integer)". Any help you could give would be appreciated! Thanks! Link to comment https://forums.phpfreaks.com/topic/158590-how-to-insert-this/#findComment-836473 Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 Something like this - INSERT INTO tablename (parent_id, section) SELECT id AS parent_id, section FROM tablename ? Obviously it's an example. Link to comment https://forums.phpfreaks.com/topic/158590-how-to-insert-this/#findComment-836498 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.