从In - >转换Sql Server查询存在?

时间:2012-08-06 08:21:22

标签: sql-server sql-server-2005

我想转换此查询:

SELECT *
FROM   myTable
WHERE   dateCreated = (
                       SELECT MAX(dateCreated)
                       FROM   myTable
                      )
      AND duty IS NULL
      AND CompanyPayingId   IN 
                                  (43081 ,43082 ,43084 ,43085)

in更改为Exists查询(由于效果):

但是我在转换代码时遇到问题,因为我认为我必须复制 where 子句......

如何转换此代码?

1 个答案:

答案 0 :(得分:1)

我可以这样做:

create table num(idn int)
insert into num values(43081) ,(43082),(43084) ,(43085)
select * from num
SELECT *
FROM   myTable m inner join num n
ON  m.dateCreated = (SELECT MAX(dateCreated) FROM   myTable)
      AND m.duty IS NULL
      AND m.CompanyPayingId=n.num