鉴于以下情况:
declare @a table
(
pkid int,
value int
)
declare @b table
(
otherID int,
value int
)
insert into @a values (1, 1000)
insert into @a values (1, 1001)
insert into @a values (2, 1000)
insert into @a values (2, 1001)
insert into @a values (2, 1002)
insert into @b values (-1, 1000)
insert into @b values (-1, 1001)
insert into @b values (-1, 1002)
如何查询@a中与@b完全匹配的所有值?
{@a.pkid = 1, @b.otherID = -1}
不会返回(3 个值中只有 2 个匹配)
{@a.pkid = 2, @b.otherID = -1}
将返回(3 个值匹配中的 3 个)
重构表可以是一种选择。
EDIT:我根据 James 和 Tom H 的答案取得了成功。
当我在 @b 中添加另一个案例时,它们有点不足。
insert into @b values (-2, 1000)
假设这应该返回另外两行({@a.pkid = 1, @b.otherID = -2}
and {@a.pkid = 2, @b.otherID = -2}
,它不起作用。然而,对于我的项目来说这不是问题。