Jump to content


Photo

Partially merge 2 tables


  • Please log in to reply
2 replies to this topic

#1 wonderer

wonderer
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 May 2006 - 09:57 PM

hello all :)

i'm quite newbie so please don't kick me if this question can look stupid :P

i am needing to populate a table copying datas from other tables

i try to explain :)

i have tables A, B, C
C is empty

structure of table A is
call_id, name, birthday, phone, message

structure of table B is
name_id, name

table C should be
name_id, call_id, message

so i have to take 3 fields from table A substituting one of them (name) with corrispondant name_id form table B

i tried like this
$q = "SELECT B.name_id FROM B , A WHERE B.name = A.name";
$query="INSERT INTO C (name_id, call_id, message) SELECT $q, call_id, message FROM A ORDER by call_id";

obviously it doesn't work [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

any idea of how to make it work?
thanks :)

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 May 2006 - 01:24 AM

Sounds like you want the following (UNTESTED):

INSERT INTO C (name_id, call_id, message) SELECT B.name_id, A.call_id, A.message FROM A JOIN B ON ( B.name = A.name )

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 wonderer

wonderer
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 29 May 2006 - 05:39 AM

[!--quoteo(post=377962:date=May 29 2006, 03:24 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 29 2006, 03:24 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sounds like you want the following (UNTESTED):

INSERT INTO C (name_id, call_id, message) SELECT B.name_id, A.call_id, A.message FROM A JOIN B ON ( B.name = A.name )
[/quote]


thanks i'm gonna try and let u know :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users