views:

780

answers:

4
Table A
Col1 Col2
101  102
101  103
102  104
104  105

Table B
Col1
101
102
103
104
105

I want to take data from Table A and insert it into Table B as a Distinct value in one query so INSERT INTO TableB (Col1) (SELECT ...)

Any ideas?

+9  A: 

You should be able to do:

 INSERT INTO TableB (Col1)
 (SELECT Col1 FROM TableA
  UNION
 SELECT Col2 FROM TableA)
pgb
+2  A: 
INSERT TABLEB (Col1)
SELECT Col1 FROM TABLEA
UNION SELECT COL2 FROM TABLEA

For non-distinct, UNION ALL

Marc Gravell
A: 

You could easily use two queries like this:

INSERT INTO `TableB` (`Col1`)
SELECT `Col1` FROM `TableA`

INSERT IGNORE INTO `TableB` (`Col1`)
SELECT `Col2` FROM `TableA`
Peter Stuifzand
A: 

insert B (col1) SELECT distinct col1 from A union select distinct col2 from A;