If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > duplicate multiple rows - but certain cols still custom

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-13-09, 23:51
ameyer ameyer is offline
Registered User
 
Join Date: Jul 2009
Posts: 32
duplicate multiple rows - but certain cols still custom [SOLVED]

I doubt this is possible, and I am successfully doing this using a php file right now, but im wondering if this is possible to do completely in sql

So say I have a table with say 6 rows (id, sid, file, file_version, timeStamp).
I want to duplicate the rows while changing the id, sid, and time

So basically I want to create X new rows, that share the file, and file_version with the original X

Code:
id	sid	file	file_version	time
//have
1	4	2	38		2009-07-12 19:42:59
2	4	13	46		2009-07-12 19:42:59
3	4	56	59		2009-07-12 19:42:59
4	4	78	72		2009-07-12 19:42:59
5	4	212	117		2009-07-12 19:42:59
6	4	300	190		2009-07-12 19:42:59

//want to add
7	13	2	38		2009-07-13 22:04:02
8	13	13	46		2009-07-13 22:04:02
9	13	56	59		2009-07-12 22:04:02
10	13	78	72		2009-07-12 22:04:02
11	13	212	117		2009-07-12 22:04:02
12	13	300	190		2009-07-12 22:04:02

------------------------------SOLVED WITH------------------------------

Quote:
Originally Posted by r937
temp table is not necessary
Code:
INSERT 
  INTO daTable
     ( sid  
     , file 
     , file_version  
     , time )
SELECT 13      -- new sid
     , file
     , file_version  
     , CURRENT_TIMESTAMP
  FROM daTable
 WHERE sid = 4  -- sid being copied

Last edited by ameyer; 07-14-09 at 08:23.
Reply With Quote
  #2 (permalink)  
Old 07-14-09, 05:55
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
Try something like this:
- create a temp table
- INSERT INTO temp_table
SELECT new_id, new_sid, file, file_version, time FROM original_table
- INSERT INTO original_table
SELECT * FROM temp_table
__________________
Florin Aparaschivei
Iasi, Romania
Reply With Quote
  #3 (permalink)  
Old 07-14-09, 08:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
temp table is not necessary
Code:
INSERT 
  INTO daTable
     ( sid  
     , file 
     , file_version  
     , time )
SELECT 13      -- new sid
     , file
     , file_version  
     , CURRENT_TIMESTAMP
  FROM daTable
 WHERE sid = 4  -- sid being copied
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-14-09, 08:21
ameyer ameyer is offline
Registered User
 
Join Date: Jul 2009
Posts: 32
WOW man! You are good!
Thank you so much.


Quote:
Originally Posted by r937
temp table is not necessary
Code:
INSERT 
  INTO daTable
     ( sid  
     , file 
     , file_version  
     , time )
SELECT 13      -- new sid
     , file
     , file_version  
     , CURRENT_TIMESTAMP
  FROM daTable
 WHERE sid = 4  -- sid being copied
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On