SQL 连接两列,其中 1 相等,另一列不相等
如何在两列上连接 2 个不同的表,其中一列相等而另一列不相等。在下面的示例中,我需要查询电子邮件匹配但 NID 不匹配的记录。
Table A
ID NID Email
1 bob [email protected]
2 mike [email protected]
3 dave [email protected]
Table B
ID NID Email
1 bob [email protected]
2 mike [email protected]
3 doug [email protected]
示例输出应来自表 B:
ID NID Email
3 doug [email protected]
如果您可以在 LINQ to Entities 中执行此操作,则可获得加分
How can I join 2 different tables on 2 columns where one column is equal and the other is not. In the example below, I need to query for records where the email matches but the NID does not.
Table A
ID NID Email
1 bob [email protected]
2 mike [email protected]
3 dave [email protected]
Table B
ID NID Email
1 bob [email protected]
2 mike [email protected]
3 doug [email protected]
Sample output should be from Table B:
ID NID Email
3 doug [email protected]
Bonus points if you can do it in LINQ to Entities
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
结果:
3 doug [电子邮件受保护]
In conclusion just a join with a WHERE condition
Result:
3 doug [email protected]
In conclusion just a join with a WHERE condition
这应该可行:
不清楚您是否只需要
A
记录,只需要B
记录,还是两者都需要。您可以根据您期望返回的数据更改SELECT
和JOIN
类型。这解决了您所描述的问题,但是如果您对包含示例数据的表运行它,您将得到一个空结果集。
This should work:
It's not clear if you want only the
A
records, only theB
records, or both. You'd change theSELECT
andJOIN
type depending on which data you're expecting back.This solves the question as you described it, but if you run it against tables containing your sample data you're going to get an empty result set.