带有 if 条件的内连接
我试图仅当表 1 的 RepID 存在于表 2 中时才编写带有内部联接的查询,如果不存在,则不联接表 2。通过我下面使用的查询,如果表 2 中不存在 repID,我就不会从两个表中获取数据。怎么可能呢?我正在使用 sql server 2005。提前谢谢您!
Select * from Table1
inner join Table2 on Table1.RepID = Table2.RepID
where Table1.Date = @Date
order by Table1.Date desc
I was trying to write a query with inner join only if RepID of Table1 exists in Table2, if not do not join table2. With the query that i used below, i do not get from both the tables if repID doesnot exist in Table2. How is it possible? I am using sql server 2005. Thank you in advance!
Select * from Table1
inner join Table2 on Table1.RepID = Table2.RepID
where Table1.Date = @Date
order by Table1.Date desc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果在连接的两侧都找到匹配项,则内部连接只会返回一行。如果您正在寻找在找到匹配项时返回 Table1 中的所有行但仅返回 Table2 中的记录的内容,那么您需要左外连接:
An inner join will only return a row if matches are found in both sides of the join. If you're looking for something that will return all rows from Table1 but only records from Table2 when a match is found, you want a left outer join:
尝试“LEFT JOIN”而不是“INNER JOIN”。
“LEFT”一词的意思是“始终包含连接左侧表中的每条记录”,在本例中为 Table1,因为您将编写:Table1 LEFT JOIN Table2,并且“Table1”位于该对的左侧! :-)
Try "LEFT JOIN" instead of "INNER JOIN".
The word "LEFT" means "Always include every record from the table on the LEFT of the join," in this case Table1, since you will write: Table1 LEFT JOIN Table2, and "Table1" is on the left of that pair! :-)
听起来您真正想要的是左外连接,不是吗?
it sounds like what you actually want is a left outer join, isnt it?
这就是外连接的用途。
That's what outer joins are for.