- 爱易网页
-
MSSQL教程
- 复杂数据查询的有关问题?不会写sql
日期:2014-05-18 浏览次数:20549 次
复杂数据查询的问题?不会写sql
SELECT TOP 40 id FROM mytable
WHERE (id NOT IN(SELECT TOP 0 id FROM mytable order by id ))
order by id
4693
4694
4696
4697
4698
4702
4703
4704
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
注:取0-40数据段,只有28个,所以查出了所有,这是正常的
SELECT TOP 20 id FROM mytable
WHERE (id NOT IN(SELECT TOP 0 id FROM mytable order by id ))
order by id
4693
4694
4696
4697
4698
4702
4703
4704
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
注:取0-20数据段,这是正常的
SELECT TOP 20 id FROM mytable
WHERE (id NOT IN(SELECT TOP 20 id FROM mytable order by id ))
order by id
4718
4719
4720
4721
4722
4723
4724
4725
注:取20-20数据段,总共28个,所以查出了剩余的8个,这是正常的
SELECT TOP 40 id FROM mytable
WHERE (id NOT IN(SELECT TOP 0 id FROM mytable order by id )) and address=0
order by id
4702
4703
4704
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4724
4725
注:取0-40数据段,并且要求address=0,总共22个,全部28个数据,剩余6个address=1,这是正常的
SELECT TOP 20 id FROM mytable
WHERE (id NOT IN(SELECT TOP 0 id FROM mytable order by id )) and address=0
order by id
4702
4703
4704
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
注:取0-20数据段,并且要求address=0,这是正常的
SELECT TOP 20 id FROM mytable
WHERE (id NOT IN(SELECT TOP 20 id FROM mytable order by id )) and address=0
order by id
4718
4719
4720
4721
4722
4724
4725
注:取20-20数据段,并且要求address=0
这里出现了问题,为什么查找出来的数据段不是剩余的4724和4725,而是有7个数据???怎么改查询
------解决方案--------------------
SELECT TOP 20 id FROM mytable
WHERE (id NOT IN(SELECT TOP 20 id FROM mytable order by id ))
order by id
4718
4719
4720
4721
4722
4723
4724
4725
注:取20-20数据段,总共28个,所以查出了剩余的8个,这是正常的
------解决方案--------------------
------------------------------try--------------------
select * from (
SELECT TOP 20 id FROM mytable
WHERE (id NOT IN(SELECT TOP 20 id FROM mytable order by id )) ) t where address=0