2016-01-20 116 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