SQL UNION INNER JOIN

时间:2013-07-03 10:28:54

标签: sql-server sql-server-2008 union inner-join union-all

我试图从2个具有相同列的表中进行选择,但两个表都有内连接 -

select e.ID, 
    c.FullName, 
    car.VIN, 
    car.Registration, 
    e.Telephone, 
    e.Mobile, 
    e.Email, 
    e.EstimateTotal, 
    e.LastUpdated, 
    e.LastUpdateBy from (select id from Estimates UNION ALL select id from PrivateEstimates) e

inner join Customers c on c.ID = e.CustomerID
inner join Cars car on car.ID = e.CarID
where e.Status = 0

麻烦的是,它在内连接上找不到e.CustomerID,e.CarID或e.Status?有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您的子查询

select id from Estimates 
union all
select id from PrivateEstimates

只返回一个id列。如果要在JOIN语句

中使用这些列,请在子查询中包含必要的列