日期:2014-05-18  浏览次数:20595 次

急:怎么为联合的两个不同的表查询结果集创建唯一ID以便分页
select tb.id,tb.title from (
select id,title from table1
union  
select id,title from table2
) td

已经创建了视图,但是必须有一个唯一ID才好分页

怎么为上面创建唯一ID以便下面分页

分页查询代码
string sql = string.Format("select top {0} * from td where title like '%{2}%' and id not in (select top {1} id from td where title like '%{2}%' order by id desc) order by id desc", pageinfo.pagerecode, (pageinfo.pagecount - 1) * pageinfo.pagerecode, serachtxt);

------解决方案--------------------
SQL code
select no=row_number() over(order by getdate()) from ...

------解决方案--------------------
SQL2000?2005?

2005:

SQL code

select *
from(
select tb.id,tb.title,px = row_number() over(order by getdate())
from (
select id,title from table1
union   
select id,title from table2
) td
)te
where px between 1 and 10

------解决方案--------------------
2005或是2008用row_number即可。
------解决方案--------------------
select row_number() over(order by id) tb.id,tb.title from (
select id,title from table1
union
select id,title from table2
) td


------解决方案--------------------
SQL code
select row_number() over(order by id) tb.id,tb.title from (
select id,title from table1
union   
select id,title from table2
) td

------解决方案--------------------
2000用identity