S
SanDao
Unregistered / Unconfirmed
GUEST, unregistred user!
兄弟我今日在试验一个SQL语句!目的是取数据库中间的一些记录!(下面的例子是取11-20条记录)
于是兄弟我是这样写的:
SELECT TOP 10 * FROM Table
WHERE (ID_Field NOT IN
(SELECT TOP 10 ID_Field
FROM Table
WHERE CangKu = '库1' order by ID_Field)) AND (CangKu = '库1')
order by ID_Field
这样写原已满足我的要求!好了!数据也是对的!
--------------
于是我想搞的更好看一点!在里面想再按照ShengChanPH排序!就这样写
SELECT TOP 10 * FROM Table
WHERE (ID_Field NOT IN
(SELECT TOP 10 ID_Field
FROM Table
WHERE CangKu = '库1' order by ShengChanPH, ID_Field)) AND (CangKu = '库1')
order by ShengChanPH,ID_Field
运行!不好,SQLServer不动了,再看CPU100%,天哪,在做什么呢!过了一会,超时!
------------------
再试了一会儿,还是不行!无意中将 CangKu = '库1'删除!
SELECT TOP 10 * FROM Table
WHERE (ID_Field NOT IN
(SELECT TOP 10 ID_Field
FROM Table
order by ShengChanPH, ID_Field))
order by ShengChanPH,ID_Field
不加这个条件居然成功了!这样一来我好像明白了什么!Not In ,把条件CangKu = '库1'
也排除了!在外面的SQL语句当然没法查了!有可能是这样(CPU100%就不明白了,
现在已经很迷茫了)!
那为什么上面写的第一个SQL语句,我也这样写为什么就对了呢!(完全糊涂了!)
--------------------------
写的有点乱,不过估计大家能够看懂!忙我想想!谢谢!
于是兄弟我是这样写的:
SELECT TOP 10 * FROM Table
WHERE (ID_Field NOT IN
(SELECT TOP 10 ID_Field
FROM Table
WHERE CangKu = '库1' order by ID_Field)) AND (CangKu = '库1')
order by ID_Field
这样写原已满足我的要求!好了!数据也是对的!
--------------
于是我想搞的更好看一点!在里面想再按照ShengChanPH排序!就这样写
SELECT TOP 10 * FROM Table
WHERE (ID_Field NOT IN
(SELECT TOP 10 ID_Field
FROM Table
WHERE CangKu = '库1' order by ShengChanPH, ID_Field)) AND (CangKu = '库1')
order by ShengChanPH,ID_Field
运行!不好,SQLServer不动了,再看CPU100%,天哪,在做什么呢!过了一会,超时!
------------------
再试了一会儿,还是不行!无意中将 CangKu = '库1'删除!
SELECT TOP 10 * FROM Table
WHERE (ID_Field NOT IN
(SELECT TOP 10 ID_Field
FROM Table
order by ShengChanPH, ID_Field))
order by ShengChanPH,ID_Field
不加这个条件居然成功了!这样一来我好像明白了什么!Not In ,把条件CangKu = '库1'
也排除了!在外面的SQL语句当然没法查了!有可能是这样(CPU100%就不明白了,
现在已经很迷茫了)!
那为什么上面写的第一个SQL语句,我也这样写为什么就对了呢!(完全糊涂了!)
--------------------------
写的有点乱,不过估计大家能够看懂!忙我想想!谢谢!