0

如何tariff_data使用 JOIN 显示已包含在 UNION 中的表中的某些字段?

例如:

SELECT  X.* FROM
    (
         SELECT phone_id, tariff_id, active  FROM table1 WHERE id = 5
      UNION
          SELECT dgt.phone_id, tns.tariff_id, dgt.active  FROM  table2 AS dgt 
                JOIN tariff_data AS tns ON tns.something = dgt.something
          WHERE id = 5
    ) as X
4

2 回答 2

1
SELECT  X.* FROM
(
    SELECT phone_id, tariff_id, active, null as "somefield"  FROM table1 WHERE id = 5
      UNION
    SELECT dgt.phone_id, tns.tariff_id, dgt.active, tns.somefield FROM  table2 AS dgt 
            JOIN tariff_data AS tns ON tns.something = dgt.something
    WHERE id = 5
) as X
于 2012-10-07T19:06:16.957 回答
1

您需要在两个 Select 语句中包含来自关税数据的字段,将第一个设置为 null 或您喜欢的任何内容:

SELECT  X.* FROM
(
    SELECT phone_id, tariff_id, active, null as "somefield"  FROM table1 WHERE id = 5
      UNION
    SELECT dgt.phone_id, tns.tariff_id, dgt.active, tns.somefield FROM  table2 AS dgt 
            JOIN tariff_data AS tns ON tns.something = dgt.something
    WHERE id = 5
) as X
于 2012-10-07T19:08:25.817 回答