Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert into select

Posted on 2004-10-19
4
Medium Priority
?
16,851 Views
Last Modified: 2012-08-13
can i have statement like this:

insert into table2
....
..
select (... ,.., ,  from table1, table3 where table1.id = table3.id)

Thanks a lot for your help
0
Comment
Question by:Vasi04
4 Comments
 
LVL 12

Assisted Solution

by:catchmeifuwant
catchmeifuwant earned 200 total points
ID: 12355053
Yes you can have it...

1)If both the table structures are same - column positions and data type then do :

insert into table2
select a.* from
table1 a, table3 b
where a.id = b.id

This will insert all rows from table1 (matching with id of table 2) into table3

2)If you want selected combination of data from both tables then do:

insert into table2(col1,col2,col3)
select a.col1, b.col2, a.col3 from
table1 a, table3 b
where a.id = b.id
0
 
LVL 16

Assisted Solution

by:GreymanMSC
GreymanMSC earned 200 total points
ID: 12355090
No.  Well, almost.  Use joins for greater efficiency.

  insert into table2(col1,col2,col3)
  select a.col1, b.col2, a.col3
  from table1 as a inner join table3 as b on a.id = b.id
0
 
LVL 19

Accepted Solution

by:
NickUpson earned 400 total points
ID: 12355348
insert into table2 (field1, field2) values
  (select fielda, fieldb from table1, table3 where table1.id = table3.id)

is ok, the exact syntax can depend upon the database you are using, just make sure the
fields match in number, type, etc
0
 

Assisted Solution

by:a_tya
a_tya earned 200 total points
ID: 12355598
You can use this if tables have the same structure:

insert into table2
select a.*
 from table1 a
INNER JOIN table3 b on  a.id = b.id


or use :
select a.*
INTO table2
 from table1 a
INNER JOIN table3 b on  a.id = b.id

IF table2 doesn't exist and you want to copy both data and table design from Table1.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question