内部加入并选择该表上的其他列

时间:2016-04-22 07:15:25

标签: sql sql-server

这是我的查询的一部分,我在此查询之前有一个联合。我想在条件下在table1中选择更多列。

SELECT  c.ID, c.Name, c.Position, c.Email, c.ContactNumber
FROM table1 c
INNER JOIN table2 a
ON c.ID = (SELECT foreignID FROM table2 WHERE a.Name = 'someName')
WHERE Dept = 'Something' --this will return nothing since in the inner join
                         --the condition returns a single column and it doesn't
                         --satisfy the WHERE Clause

我想选择满足ID(有效)的人,包括部门 Something 的人。有没有另外一种解决方法,或者我真的需要使用UNION。这会影响应用程序的性能,特别是移动应用程序吗?

1 个答案:

答案 0 :(得分:1)

如果我理解了您的要求,请使用LEFT JOIN

SELECT  c.ID, c.Name, c.Position, c.Email, c.ContactNumber
FROM table1 c
LEFT JOIN table2 a ON c.ID = a.foreignID AND  a.Name = 'someName'
WHERE Dept = 'Something'