日期:2014-05-19  浏览次数:20510 次

问条简单的语法.
selet   top   5   id,name   from   table   where   id=1

我想这样做,如果数据表里的id=1的记录小于5条,就显示id等于其它值的!~~要排序应该怎样做呢??

例如:table里,id=1的只有一条记录,我想把这条记录放到最前,再显示其它id,按排序显示够5条....不知说得清不清楚...

------解决方案--------------------
set rowcount 5
select id, name from tbName where id=1
union all
select id, name from tbName where id <> 1
set rowcount 0

------解决方案--------------------
如果ID=1是最小值

SELECT TOP 5 id,name from [table] order by id

如果ID=1不是最小值

SELECT TOP 5 id,name FROM (SELECT 1 AS N,id,name from [table] WHERE ID=1 UNION ALL
SELECT 2 AS N,id,name from [table] where id <> 1) A
ORDER BY N
------解决方案--------------------
id=1的只有一条记录,我想把这条记录放到最前,再显示其它id

其他的id随便??

selet top 5 id,name from table
order by case id when 1 then 0 else 1 end

------解决方案--------------------
一楼正确的
------解决方案--------------------

selet top 5 id,name from table
where id> =1
order by id
------解决方案--------------------
set rowcount 5
select id, name from tbName where id=1
union all
select id, name from tbName where id <> 1 group by [日期字段]
set rowcount 0
------解决方案--------------------


------解决方案--------------------
不太理解,和尚的简单做法:
selet top 5 id,name from table
where id> =1
order by id

不是你想要的吗?