没有虚拟列的不同字段上的MySQL联盟

时间:2012-04-19 23:29:19

标签: mysql join field union

因此,假设我有两个或更多表由不同的列组成,其中不一定存在共享密钥(id):

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 |
+----+---------+--------+

期望的效果是在id插入NULL时将表混合在一起,其中数据不可用:

+----+-------+-------+-------+---------+-----+------------+------+---------+--------+
| 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 |
+----+-------+-------+-------+---------+-----+------------+------+---------+--------+

我可以使用NULL'虚拟'列和UNION(MySql SELECT union for different columns?)但如果表的数量很大,这似乎是一种皇家的痛苦。我想我可以用一种JOIN方法来实现这个目标,但我需要一些帮助来解决这个问题。

这有效:

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` ;

但确实感觉就像错误这样做。当我想要在其他表上添加时,如何在不必编辑插入NULL AS whatever的行和行的情况下获得相同的结果?

提前致谢!

2 个答案:

答案 0 :(得分: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

如果表除了id之外不共享其他列,则可以使用简单的写入(但更容易破解):

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 

答案 1 :(得分:0)

您想使用LEFT JOIN s。

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

在你的例子中:

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