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

Rewrite T-SQL from using IN to using JOIN/WHERE(将 T-SQL 从使用 IN 改写为使用 JOIN/WHERE)
本文介绍了将 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert varchar to float IF ISNUMERIC(将 varchar 转换为 float IF ISNUMERIC)
multi part identifier could not be bound sql(无法绑定多部分标识符 sql)
Any benefit to explicitly dropping local temporary tables at the end of a stored procedure?(在存储过程结束时显式删除本地临时表有什么好处?)
Check if a column contains text using SQL(使用 SQL 检查列是否包含文本)
Select value if condition in SQL Server(SQL Server 中的条件选择值)
Control flow in T-SQL SP using IF..ELSE IF - are there other ways?(使用 IF..ELSE IF 在 T-SQL SP 中控制流 - 还有其他方法吗?)