读书人

请教为什么用 order by newid() 随机产

发布时间: 2012-01-19 20:57:58 作者: rapoo

请问为什么用 order by newid() 随机产生记录的时候,那些image字段不为空的记录无法被检索出来?
请问为什么用 order by newid() 随机产生记录的时候,那些image字段不为空的记录无法被检索出来?

我的表:
id int, type as int, img as image
id 是主键

我用
SELECT TOP 45 id, type, main
FROM subjects
WHERE type = 0
order by newid()

而当我把所有 img为空的数据都删掉的时候,就可以检索出img有数据的记录了
请问这是为什么? 怎么解决,谢谢

[解决办法]
数据量有多大啊
[解决办法]
那你加一个条件试试啊

SELECT TOP 45 id, type, main
FROM subjects
WHERE type = 0 and (img is not null or img != ' ')
order by newid()

[解决办法]
--没有发现此问题。

create table T1(id int,img image)

insert into T1 select 1,null
insert into T1 select 2,null
insert into T1 select 3,null

insert into T1 select 3,0x1F
insert into T1 select 3,0x2F

select top 2 *
from T1
order by newid()

drop table T1

[解决办法]
如果是SQLSERVER2000的话,请检查一下是否打了SP4补丁.如果没有,请打上SQLSERVER2000 SP4补丁再试试.
[解决办法]
先看看你的两个union之前的select里是否就满足条件
[解决办法]
楼主看看
SELECT TOP 45 id, main, type, answer
FROM subjects
WHERE type = 0
UNION
SELECT TOP 55 id, main, type, answer
FROM subjects
WHERE type = 1
这部分查询出来了什么数据

理论上应该不会有问题的啊,跟image类型因该没有什么关系
[解决办法]
SELECT *
FROM (SELECT TOP 45 id, main, type, answer
FROM subjects
WHERE type = 0 and DATALENGTH (img)> 0
UNION
SELECT TOP 55 id, main, type, answer
FROM subjects
WHERE type = 1 and DATALENGTH (img)> 0) DERIVEDTBL
ORDER BY type, NEWID()

读书人网 >SQL Server

热点推荐