把not in改为not exists select top 20 id,mingzi from [my_user] where id not in (SELECT top 40 id FROM [my_user] order by cdate desc) order by cdate desc
麻烦高手改成效率更高的语句!听说not exists要走索引的!谢谢!
selectnot?in
分享到:
------解决方案--------------------
select top 20 a.id,a.mingzi
from [my_user] a
left join (SELECT top 40 id FROM [my_user] order by cdate desc) b on a.id=b.id
where a.id is null
order by cdate desc
------解决方案--------------------
SELECT TOP 20
id , mingzi
FROM [my_user]
WHERE NOT EXISTS ( SELECT 1
FROM (
SELECT TOP 40
id
FROM [my_user]
ORDER BY cdate DESC
) t
WHERE id = t.id )
ORDER BY cdate DESC