954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

inserting data into a third table

i want to insert data into third table for two tables
that is first table:table struct: pid -->int,name-->string ..etc
second table :cid -->int,parentid-->int....etc
third table struct is:pid-->int,cid-->int
now iwant to insert data into third table from the other two tables
pid from first table is a prime key and pid from third is a foreign key to it
cid-->prime key and cid in third table is foreign to it.
i.

srpa01red
Newbie Poster
19 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Hi srpa01red and welcome to DaniWeb,

This is certainly doable. But I have to ask, if the third table only contains those two fields, is it necessary to create the third table? From the looks of the structure of the second table, you already have that information in that table. Or am I misunderstanding what you are trying to accomplish here?

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

You can do it in following manner
insert into table3 (pid,cid) select a.pid,b.cid from table1 a cross join table2 b

As darkagn said, your case doesnt need this to be done. Its already there

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

actually the two tables are imported and want to insert the data into third tablei.e pid from first table and cid from second table

srpa01red
Newbie Poster
19 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
actually the two tables are imported and want to insert the data into third tablei.e pid from first table and cid from second table

By "imported" do you mean that they are in a different database? Is the database on another MySQL server?

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 
By "imported" do you mean that they are in a different database? Is the database on another MySQL server?


the data is imported from a .csv file

srpa01red
Newbie Poster
19 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

But if the data is imported into the first two tables, I don't think you need the third table. Table 2 has the information that you require in Table 3, so you would just be duplicating data.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You