1

So lets say I have 2 or more tables consisting of dissimilar columns in which a shared key (id) is not necessarily present :

Alpha:  
+----+-------+-------+-------+
| id | paula | randy | simon |
+----+-------+-------+-------+
|  1 |     8 |     7 |     2 |
|  2 |     9 |     6 |     2 |
|  3 |    10 |     5 |     2 |
+----+-------+-------+-------+

Beta:
+----+---------+-----+------------+------+
| id | is_nice | sex |        dob | gift |
+----+---------+-----+------------+------+
|  2 |       1 |   F | 1990-05-25 | iPod |
|  3 |       0 |   M | 1990-05-25 | coal |
+----+---------+-----+------------+------+

Gamma:
+----+---------+--------+
| id | is_tall | is_fat |
+----+---------+--------+
|  1 |       1 |      1 |
| 99 |       0 |      1 |
+----+---------+--------+

The desired effect is to mash the tables together on id inserting NULLs where data is not available:

+----+-------+-------+-------+---------+-----+------------+------+---------+--------+
| id | paula | randy | simon | is_nice | sex |        dob | gift | is_tall | is_fat |
+----+-------+-------+-------+---------+-----+------------+------+---------+--------+
|  1 |     8 |     7 |     2 |         |     |            |      |       1 |      1 |
|  2 |     9 |     6 |     2 |       1 |   F | 1990-05-25 | iPod |         |        |
|  3 |    10 |     5 |     2 |       0 |   M | 1990-05-25 | coal |       1 |      1 |
| 99 |       |       |       |         |     |            |      |       0 |      0 |
+----+-------+-------+-------+---------+-----+------------+------+---------+--------+

I can use NULL 'dummy' columns and UNION (MySql SELECT union for different columns?) but that seems like a royal pain if the number of tables is great. I'd like to think there is a JOIN method I can use to accomplish this, but I need some help to figure this out.

This works:

SELECT `id`, `paula`, `randy`, ..., NULL AS `is_nice`, ... FROM `Alpha`
UNION SELECT `id`, NULL AS `paula`, ..., FROM `Beta`
UNION SELECT `id`, NULL AS `paula`, ..., `is_fat` FROM `Gamma` ;

but it sure feels like the wrong way to do it. How can I get the same results without having to edit lines and lines of SQL inserting NULL AS whatever all over the place whenever I want to tack on additional tables?

Thanks in advance!

1
  • Actually, your query would not work like that. You'd get 7 rows in the result. Apr 19, 2012 at 23:43

2 Answers 2

1
SELECT
    allid.id
  , a.paula, a.randy a.simon
  , b. ...
  , c. ... 
FROM
        ( SELECT id
          FROM Alpha
        UNION 
          SELECT id
          FROM Beta
        UNION 
          SELECT id
          FROM Gamma 
        ) AS allid
    LEFT JOIN
        Alpha AS a
            ON a.id = allid.id
    LEFT JOIN
        Beta AS b
            ON b.id = allid.id
    LEFT JOIN
        Gamma AS g
            ON g.id = allid.id

If the tables share no other column except the id, you could use the simple to write (but easier to break):

SELECT 
    *
FROM
        ( SELECT id
          FROM Alpha
        UNION 
          SELECT id
          FROM Beta
        UNION 
          SELECT id
          FROM Gamma 
        ) AS allid
    NATURAL LEFT JOIN
        Alpha
    NATURAL LEFT JOIN
        Beta 
    NATURAL LEFT JOIN
        Gamma 
0

You want to use LEFT JOINs.

http://dev.mysql.com/doc/refman/5.0/en/join.html

In your example:

SELECT id_t.id, a.paula, a.randy, a.simon, b.is_nice, b.sex, b.dob, b.gift, g.is_tall, g.is_fat
FROM (SELECT DISTINCT id FROM alpha,beta,gamma) as id_t
LEFT JOIN alpha a ON a.id = id_t.id
LEFT JOIN beta b on b.id = id_t.id
LEFT JOIN gamma g on g.id = id_t.id
1
  • OK, but it looks like ypercube might have been first.
    – Mark Byers
    Apr 19, 2012 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.