1

因此,假设我有 2 个或更多由不同列组成的表,其中不一定存在共享密钥(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 'dummy' 列和 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` ;

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

提前致谢!

4

2 回答 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

如果这些表除了 之外没有其他列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 
于 2012-04-19T23:36:02.420 回答
0

您想使用LEFT JOINs。

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
于 2012-04-19T23:33:01.043 回答