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 > insert from one table into another

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-21-07, 03:19
esthera esthera is offline
Registered User
 
Join Date: Jun 2006
Posts: 66
insert from one table into another

i have


insert into membersarchive
select firstname,lastname from members


how can i do this but I also want to add an additional field not in members called username and add this to the values being inserted?
Reply With Quote
  #2 (permalink)  
Old 05-21-07, 05:02
ashish_mat1979 ashish_mat1979 is offline
Registered User
 
Join Date: Aug 2005
Posts: 30
You have to specify from where you want to pick username value and fieldnames explicitly in Insert into query; it will look somewhat like this, where users is another table for example from where you have to pick username:

insert into membersarchive (firstname,lastname,username)
select firstname,lastname,username from members,users where members.userid=users.userid
__________________
Ashish
Entertainment Overloaded
Reply With Quote
  #3 (permalink)  
Old 05-21-07, 05:26
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by esthera
I also want to add an additional field not in members called username and add this to the values being inserted
Where is this extra value held?
Without knowing this information we can't help you!
Here's a quick quess
Code:
INSERT INTO membersarchive (firstname,lastname,username)
SELECT	 firstname
	,lastname
	,Left(surname,6) + Left(firstname,2)
FROM	 members
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 05-21-07, 05:42
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Where are you getting the variable username from? Is it coming in from a script? Is it coming from another table? Do you want to insert a blank?

Blank :
Code:
INSERT INTO 
  membersarchive(firstname,lastname,username)
SELECT firstname,lastname,'' FROM members
Variable from script (php) (foreach user)
Code:
INSERT INTO 
  membersarchive(firstname,lastname,username)
SELECT firstname,lastname,'{$php_username}' FROM members WHERE <some clause>
From another table
Code:
INSERT INTO
  membersarchive (firstname,lastname,username)
SELECT firstname,lastname,username from members m
JOIN users u ON m.u=u.userid
Reply With Quote
  #5 (permalink)  
Old 05-21-07, 05:57
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Tiny amendment in Aschk's code (aliases)
Code:
INSERT INTO membersarchive(firstname,lastname,username)
SELECT	m.firstname, m.lastname, u.username
FROM	members m
JOIN users u
	ON m.u=u.userid
__________________
George
Twitter | Blog
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