gourishankar
gourishankar

Reputation: 123

How can I fill data in table from two other tables

I am trying to convert

table1

id    name
1     aaa
2     bbb
3     ccc

and

table2

id  lastname   address
1   bbbb       asd
2   aaaa       asda
3   aaaa       asdsd
4   aaaa       asdsfd

to

table3

id    Fname     lName      address
1     aaa       bbbb       asd
2     bbb       aaaa       asda
3     ccc       aaaa       asdsd

Is it possible to do using SQL query and stored procedure?

Upvotes: 0

Views: 67

Answers (3)

mvp
mvp

Reputation: 116078

Simply use JOIN:

SELECT a.name fname,
       b.lastname lname,
       b.address
FROM table1 a
JOIN table2 b ON a.id = b.id

You can also create table3 as follows:

CREATE TABLE table3 AS
SELECT a.name fname,
       b.lastname lname,
       b.address
FROM table1 a
JOIN table2 b ON a.id = b.id

This works in almost all known database engines, except for MSSQL (SQLFiddle demo). In MSSQL, use SELECT ... INTO ... instead:

SELECT a.name fname,
       b.lastname lname,
       b.address
INTO table3
FROM table1 a
JOIN table2 b ON a.id = b.id

Upvotes: 2

Taicho
Taicho

Reputation: 143

Use Cross Join. http://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx

Select * into tableWhatever from A
CROSS JOIN B

You should research a little bit more before posting.

Upvotes: 0

lordkain
lordkain

Reputation: 3109

You can do a few things:

  1. make a select statement
  2. make an stored procedure
  3. make a view

what kind of database you are using?

Upvotes: 0

Related Questions