2016-01-20 36 views
1

相同的字段值我有拖表這樣Mysql的連接兩個表獲得基於條件

表一

id email firstname lastname 
1 [email protected] xx   xx  
2 [email protected] xb   ab  

表B

id  email firstname lastname 
    1  [email protected] sd   cx  
    2  [email protected] df   dr   

我想喜歡這個

email  firstname  lastname 

    [email protected]  xx   xx 
    [email protected]  xb   ab 
    [email protected]  df   dr 

任何人請幫助我。我試過工會,明顯沒有得到我的結果

+0

在你的表中,有名字和姓氏的不同值電子郵件地址爲[email protected]。不應該糾正? –

+2

向我們展示您已經測試 – bdn02

+0

的表名和姓氏不同的select union。 – srivathi

回答

0

你可以試試這個: -

SELECT email, firstname, lastname 
FROM table_a 
UNION 
SELECT email, firstname, lastname 
FROM table_b 
WHERE email NOT IN (SELECT email FROM table_a) 
0

你可以做

select email, firstname, lastname from a 
union all 
select email, firstname, lastname from b 
where email not in (select email from a) 
0
SELECT email 
    , firstname 
    , lastname 
    FROM table_a 
UNION 
SELECT b.email 
    , b.firstname 
    , b.lastname 
    FROM table_b b 
    LEFT 
    JOIN table_a a 
    ON a.email = b.email 
WHERE a.email IS NULL; 
+0

感謝大家,工作正常 – srivathi