Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: FULL OUTER JOIN - How do I merge one column?

I have a question regarding a FULL OUTER JOIN in MySQL. I have two (or more tables):

 table1      table2 id  value   id  value2 1   a       1   b 2   c       3   d 3   e       4   f 

I have used this query to get my join:

SELECT *  FROM table1 LEFT OUTER JOIN table2 ON table1.`id`=table2.`id` UNION SELECT *  FROM table1 RIGHT OUTER JOIN table2 ON table1.`id`=table2.`id` 

to get:

 id   value1  id   value2  1    a       1    b 2    c       NULL NULL 3    e       3    d NULL NULL    4    f 

My problem is that I don't manage to simultaneously collapse the two id columns into one column to get this:

 id   value1  value2  1    a       b 2    c       NULL 3    e       d 4    NULL    f 

Any suggestions on how to do it?

like image 776
Mig Cervantez Avatar asked Dec 11 '10 03:12

Mig Cervantez


People also ask

Can we use full outer join in MySQL?

MySQL doesn't offer syntax for a full outer join, but you can implement one using the union of a left and a right join.

How does a full outer join work?

An full outer join is a method of combining tables so that the result includes unmatched rows of both tables. If you are joining two tables and want the result set to include unmatched rows from both tables, use a FULL OUTER JOIN clause. The matching is based on the join condition.

Is full join same as full outer join?

The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records. Tip: FULL OUTER JOIN and FULL JOIN are the same.

Does full outer join have duplicates?

From what you are saying, the 2 tables you are comparing are more or less the same, and full outer join giving you records from both tables, chances are you are going to get a lot of duplicates. So, that's the logic behind it.


2 Answers

SELECT  COALESCE(t1.id, t2.id) as id, t1.value1, t2.value2 FROM table1 t1 FULL JOIN table2 t2 ON t1.id = t2.id; 
like image 93
Beau Avatar answered Sep 19 '22 03:09

Beau


Use:

    SELECT t1.id,            t1.value,            t2.value2        FROM table1 t1  LEFT JOIN table2 t2 ON t2.id = t1.id UNION     SELECT t2.id,            t1.value,            t2.value2       FROM TABLE1 t1 RIGHT JOIN TABLE2 t2 ON t2.id = t1.id 

The UNION operator removes row/record duplicates, so you have to define/list the columns appropriately.

Scripts:

DROP TABLE IF EXISTS `example`.`table1`; CREATE TABLE  `example`.`table1` (   `id` int(10) unsigned NOT NULL default '0',   `value` varchar(45) NOT NULL default '' ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  INSERT INTO table1 VALUES (1, 'a'), (2, 'c'), (3, 'e');  DROP TABLE IF EXISTS `example`.`table2`; CREATE TABLE  `example`.`table2` (   `id` int(10) unsigned NOT NULL default '0',   `value2` varchar(45) NOT NULL default '' ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  INSERT INTO table2 VALUES (1, 'b'), (3, 'd'), (4, 'f'); 

Edit: Fixed line above

like image 35
OMG Ponies Avatar answered Sep 23 '22 03:09

OMG Ponies