Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL INSERT INTO from multiple tables

Tags:

sql

sql-server

this is my table 1:

NAME       AGE        SEX        CITY             ID  Clara      22         f          New York         1 Bob        33         m          Washington       2 Sam        25         m          Boston           3 

this is my table 2:

NUMBER       ID 555-1111     1 555-2222     2 555-3333     3 

and now I want a table 3 which shows me all information:

NAME       AGE        SEX        CITY             ID        NUMBER  Clara      22         f          New York         1         555-1111 Bob        33         m          Washington       2         555-2222 Sam        25         m          Boston           3         555-3333 

I tried first to insert into table 3 only the values from table 1 and then I inserted into table 3 the values from table 2 with an inner join where Id = Id is.

INSERT INTO table3 { name, age, sex, city, id} SELECT name, age, sex, city, id FROM table 1    INSERT INTO table3 { name, age, sex, city, id, number} SELECT name, age, sex, city, id, number FROM table 2 p INNER JOIN table 3 c ON c.Id = p.Id 

But all I get is a duplication of my values. instead of having 3 entries, I have like 9 entries, which some have number null, some have only the number and the rest null, and some are correct.

I hope someone can help me

EDIT

If I am having now a third Table like this one:

NATIONALITY       ID  Canadian          1 American          2 French            3 

How could I merge all 3 tables into one Table?

like image 213
Paparis Avatar asked Dec 05 '13 15:12

Paparis


People also ask

How do I insert data from multiple tables into one table?

Example 5: INSERT INTO SELECT statement with Join clause to get data from multiple tables. We can use a JOIN clause to get data from multiple tables. These tables are joined with conditions specified with the ON clause. Suppose we want to get data from multiple tables and insert into a table.

Can you insert into multiple tables at once in SQL?

The T-SQL function OUTPUT, which was introduced in 2005, can be used to insert multiple values into multiple tables in a single statement. The output values of each row that was part of an INSERT, UPDATE or DELETE operation are returned by the OUTPUT clause.

Can we insert data in two tables using single query?

You can't use INSERT against two tables in one statement.

How do I combine data from multiple tables into one table in SQL?

SQL JOIN. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.


1 Answers

You only need one INSERT:

INSERT INTO table4 ( name, age, sex, city, id, number, nationality) SELECT name, age, sex, city, p.id, number, n.nationality FROM table1 p INNER JOIN table2 c ON c.Id = p.Id INNER JOIN table3 n ON p.Id = n.Id 
like image 63
D Stanley Avatar answered Oct 08 '22 22:10

D Stanley