Vinayak Pahalwan
Vinayak Pahalwan

Reputation: 3005

Merge data from two tables into single column of another table

How to merge data from multiple tables into single column of another table.

Example:

Table A

Col1 | Col2 | Col3
10
20

Table B

Col1 | Col2 | Col3
13
99

I want my o/p in Table C in Col1 as

Col1
10
20
13
99

I did (part of query)

Select Col1 from A
Union
Select Col1 from B

but it is not giving me this desired result

Upvotes: 0

Views: 1141

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

The SELECT appears correct (you may want to use UNION ALL instead of UNION to avoid elimination of duplicates).

If you want the results to be in the third table C, you need to make an INSERT from your SELECT, like this:

INSERT INTO C (Col1)
(
    SELECT Col1 from A
UNION ALL
    SELECT Col1 from B
)

Upvotes: 4

Related Questions