select top 10 *
from ta
order by case when chk = 1 then 0 else 1 end ,NEWID( )
from ta
order by case when chk = 1 then 0 else 1 end ,NEWID( )
解决方案 »
- c# 数据结构的问题
- 关于sp_addrolemember
- 我的数据值有可能是从1.0到 9999999999.9999999,请问用什么数据类型,怎么我试用了好多类型都没有小数位怎么回事?
- 急,怎样写一个sql语句,在数据库的表中只显示当月的数据,其他月份的备份
- 求一条SQL语句,在线等待,马上结帖...
- 为什么服务器升级了WIN2000补丁后,SQL SERVER 数据库就连接不上?
- 急呀!误删除
- SQL 2005版本问题, 大家快来抢分呀!!!!(100分喔)
- 如何向SQL SERVER2000的image数据列中写入超过20兆的数据?
- SqlServer2000中关于四舍五入函数 Round 的问题?急急急!!!!!
- 本地数据库与远程数据库之间表数据的复制问题,求各位大虾帮忙? happyflystone
- 数据库连接字符串
select top 10 *
from ta
order by case when chk = 1 then 0 else 1 end ,NEWID( )楼上正解
from ta
order by case when chk = 1 then 0 else 1 end
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'
select top 10 * from @tb order by case when class=1 then 0 else 1 end2 1 b
1 1 a
4 1 d
2 1 b
1 1 a
4 1 d
5 2 c
3 2 a
5 2 c
3 2 a
不过我用了这个语句为什么每次取出来的结果数目却是不对的呢??SELECT * FROM #tabletmp a
WHERE id IN
(SELECT top 6 id
FROM #tabletmp
WHERE sclassid = a.sclassid
ORDER BY (case when p0_chk=1 then 0 else 1 end),newid())
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 3,2,'c'
insert into @tb select 6,4,'c'
insert into @tb select 8,5,'c'
insert into @tb select 33,6,'c'
insert into @tb select 333,8,'c'select top 10 * from (
select * from @tb where class = 1
union all
select * from @tb where class <> 1 ) T
order by case when class = 1 then 0 else 1 end,newid()