2013-10-24 104 views
-1

我有3個表,其中包含非常相似的信息。這些表格是業主,經理和員工。我試圖從所有3張表中提取信息,同時仍然瞭解每條記錄來自哪個表。我也不想重複。所有表格都有一個email_id,它與我加入的email表相關。MySQL:從多個表中選擇字段來顯示記錄來自哪個表

另外,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'

有關如何完成此任何想法?如果您需要更多信息,請點評。謝謝!

回答

1

添加「表格名」列每組結果來確定哪些表中的結果從

0123來了
(
    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選擇塊

+0

可選地添加3列的結果的每個塊。即添加* is_owner,is_manager,is_employee *到*全部* 3選擇塊 – vogomatix

+0

您甚至可以刪除括號! –

+0

我實際上已經嘗試爲每個結果塊添加3列,並且它不能正常工作。你能舉一個這樣的例子嗎? – Chris

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 
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; 
相關問題