将 T-SQL 从使用 IN 改写为使用 JOIN/WHERE

时间:2023-02-21
本文介绍了将 T-SQL 从使用 IN 改写为使用 JOIN/WHERE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能重写这个 t-sql 以避免使用 IN - 而是使用连接?

How can I rewrite this t-sql to avoid using IN - and use joins in stead?

--Members with membership in 2008 but not in 2009, using IN and NOT IN 
SELECT * FROM @Members 
WHERE MemberId NOT IN 
 (SELECT MemberId FROM @Memberships WHERE [Year] = 2009) AND 
 MemberId IN (SELECT MemberId FROM @Memberships WHERE [Year] = 2008 )

我想找到在 2008 年有会员资格而在 2009 年没有会员资格的人.以下示例中的正确解决方案是 George 和 Bill

I want to find people with a membership in 2008, who do not have a membership in 2009. The correct solution in the below example is George and Bill

这样的东西...:

--Members with membership in 2008 but not in 2009, using IN and NOT IN 
SELECT * FROM @Members m
 INNER JOIN @Memberships msOld ON msOld.MemberId = m.MemberId AND [Year] = 2008
 PLAYTHEMOFFKEYBOARDCAT JOIN @... HUh?

完整的模式可以玩.:)

DECLARE @Members TABLE

(
  MemberId INT,
  Name VARCHAR(10)
)

INSERT INTO @Members VALUES (1, 'Joe')
INSERT INTO @Members VALUES (2, 'Mike')
INSERT INTO @Members VALUES (3, 'George')
INSERT INTO @Members VALUES (4, 'Bill')
INSERT INTO @Members VALUES (5, 'Kyle')
INSERT INTO @Members VALUES (6, 'Greg')

DECLARE @Memberships TABLE
(
  MembershipId INT,
  MemberId INT,
  [Year] INT
)

INSERT INTO @Memberships VALUES (1, 1, 2009)
INSERT INTO @Memberships VALUES (2, 1, 2007)
INSERT INTO @Memberships VALUES (3, 1, 2006)
INSERT INTO @Memberships VALUES (4, 2, 2008)
INSERT INTO @Memberships VALUES (5, 2, 2009)
INSERT INTO @Memberships VALUES (6, 3, 2007)
INSERT INTO @Memberships VALUES (7, 3, 2008)
INSERT INTO @Memberships VALUES (8, 4, 2008)
INSERT INTO @Memberships VALUES (9, 5, 2007)
INSERT INTO @Memberships VALUES (10, 5, 2006)
INSERT INTO @Memberships VALUES (11, 5, 2005)
INSERT INTO @Memberships VALUES (12, 6, 2007)

--SELECT * FROM @Members m INNER JOIN @Memberships ms ON m.MemberId = ms.MemberId ORDER BY [Year]

--Membership in 2008
SELECT MemberId FROM @Memberships WHERE [Year] = 2008

--Members with no membership in 2009
SELECT * FROM @Members WHERE MemberId NOT IN 
    (SELECT MemberId FROM @Memberships WHERE [Year] = 2009)

--Members with membership in 2008 but not in 2009, using IN and NOT IN 
SELECT * FROM @Members WHERE MemberId NOT IN 
    (SELECT MemberId FROM @Memberships WHERE [Year] = 2009) AND MemberId IN (SELECT MemberId FROM @Memberships WHERE [Year] = 2008 )

--Members with membership in 2008 but not in 2009, using IN and NOT IN 
SELECT * FROM @Members m
    INNER JOIN @Memberships msOld ON msOld.MemberId = m.MemberId AND [Year] = 2008

推荐答案

--Members with membership in 2008 but not in 2009, using JOINs
SELECT M1.*
FROM @Members M1
JOIN @Memberships MS08 ON (M1.MemberId = MS08.MemberId AND MS08.Year = 2008)
LEFT JOIN @Memberships MS09 ON (M1.MemberId = MS09.MemberId AND MS09.Year = 2009)
WHERE MS09.MemberId IS NULL

这篇关于将 T-SQL 从使用 IN 改写为使用 JOIN/WHERE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:从不同的结果中选择后获取身份 下一篇:如何插入每两行的减法并将其插入新列

相关文章

最新文章