将 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)