如何在SQL Server 2008中包含缺少数据的行

时间:2015-03-06 23:42:45

标签: sql sql-server-2008

我们假设我有一张表employees和另一个employee_address

此外,我还有employee_email_address表(员工不必发送电子邮件)

select 
    employee.emp_id, 
    employee_address.address, 
    employee_email_address.email
from  
    employees 
inner join
    employee_address on employee.emp_id = employee_address.emp_id
inner join 
    employee_email_address on employee_email_address.emp_id = employee.emp_id

但是这个查询给我的只是拥有地址和电子邮件地址的员工....

如何为员工添加没有email_address的行? (注意,电子邮件地址不为空)

感谢。

2 个答案:

答案 0 :(得分:1)

使用左连接而不是内连接。

答案 1 :(得分:1)

尝试此查询:

Select e.emp_id, ea.address, eea.email
from employees e left join
     employee_address ea
     on ea.emp_id = e.emp_id left join
     employee_email_address eea 
     on eea.emp_id = e.emp_id;