MySQL:从带有字段的多个表中选择,以显示记录来自哪个表

时间:2013-10-24 16:02:37

标签: mysql sql union multiple-tables

我有3个表,其中包含非常相似的信息。这些表是所有者,经理和员工。我试图从所有3个表中提取信息,同时仍然了解每个记录来自哪个表。我也不想重复。所有表都有一个email_id,它与我加入的email表有关。

此外,email_id很可能会存在于多个表格中(即电子邮件可能是所有者和管理者,或者甚至是所有3个)。

这是我到目前为止所做的:

SELECT email
FROM (
    (
        SELECT e.email
        FROM `owners` as o
        LEFT JOIN `email` as e
        ON e.email_id = o.email_id
        WHERE o.company_id = 3
        GROUP BY e.email
    )
    UNION ALL
    (
        SELECT e.email
        FROM `managers` as m
        LEFT JOIN `email` as e
        ON e.email_id = m.email_id
        WHERE m.company_id = 3
        GROUP BY e.email
    )
    UNION ALL
    (
        SELECT e.email
        FROM `employees` as emp
        LEFT JOIN `email` as e
        ON e.email_id = emp.email_id
        WHERE v.company_id = 3
        GROUP BY e.email
    )
) as `people`
ORDER BY email ASC

此查询实际上非常有效。它会单独从表中删除重复项,但会将它们保存在多个表中。我仍然无法知道每个表来自哪个表。我试图将查询修改为:

SELECT email, owner, manager, employee
FROM (
    (
        SELECT e.email, 'yes' as owner
        FROM `owners` as o
        LEFT JOIN `email` as e
        ON e.email_id = o.email_id
        WHERE o.company_id = 3
        GROUP BY e.email
    )
    UNION ALL
    (
        SELECT e.email, 'yes' as manager
        FROM `managers` as m
        LEFT JOIN `email` as e
        ON e.email_id = m.email_id
        WHERE m.company_id = 3
        GROUP BY e.email
    )
    UNION ALL
    (
        SELECT e.email, 'yes' as employee
        FROM `employees` as emp
        LEFT JOIN `email` as e
        ON e.email_id = emp.email_id
        WHERE v.company_id = 3
        GROUP BY e.email
    )
) as `people`
ORDER BY email ASC

这不起作用,因为并非所有表都具有相同的列。如果我将所有字段添加到所有表(即SELECT e.email, 'yes' as owner, '' as manager, '' as employee),则查询有效,但所有表似乎都具有相同的字段。不知道为什么。

最终,我希望在返回的记录集中没有重复的电子邮件地址,并且每个记录都有email = 'email address', owner = 'yes/no', manager = 'yes/no', employee = 'yes/no'

关于如何实现这一目标的任何想法?如果您需要更多信息,请发表评论。谢谢!

3 个答案:

答案 0 :(得分:1)

为每组结果添加“table_name”列,以标识结果来自哪个表

(
    SELECT e.email, 'owners' as table_name
    FROM `owners` as o
    LEFT JOIN `email` as e
    ON e.email_id = o.email_id
    WHERE o.company_id = 3
    GROUP BY e.email
)
UNION ALL
(
    SELECT e.email, 'managers' as table_name
    FROM `managers` as m
    LEFT JOIN `email` as e
    ON e.email_id = m.email_id
    WHERE m.company_id = 3
    GROUP BY e.email
)
UNION ALL
(
    SELECT e.email, 'employees' as table_name
    FROM `employees` as emp
    LEFT JOIN `email` as e
    ON e.email_id = emp.email_id
    WHERE v.company_id = 3
    GROUP BY e.email
)

或者,为每个结果块添加3列。即将* is_owner,is_manager,is_employee *添加到所有 3个选择块

答案 1 :(得分:0)

您可以通过以这种方式强制每行中的值来实现:

select t1.field1, t1.field2, 'YourFirstTable' as fromTable from t1
union all
select t2.field1, t2.field2, 'YourSecondTable' from t2

如果你想从整个结果集中删除重复项(通过“删除重复项”,我知道你想要来自任何表的电子邮件),你应该将它包装在外部查询中并应用一组:

select field1, field2, max(fromTable)
    select t1.field1 as field1, t1.field2 as field2, 'YourFirstTable' as fromTable from t1
    union all
    select t2.field1, t2.field2, 'YourSecondTable' from t2
group by field1, field2

答案 2 :(得分:0)

        SELECT e.email,"owners" AS people
        FROM `owners` as o
        LEFT JOIN `email` as e
        ON e.email_id = o.email_id
        WHERE o.company_id = 3
        GROUP BY e.email
     UNION ALL
        SELECT e.email,"managers" AS people
        FROM `managers` as m
        LEFT JOIN `email` as e
        ON e.email_id = m.email_id
        WHERE m.company_id = 3
        GROUP BY e.email
    UNION ALL
        SELECT e.email,"employees" AS people
        FROM `employees` as emp
        LEFT JOIN `email` as e
        ON e.email_id = emp.email_id
        WHERE v.company_id = 3
        GROUP BY e.email;