读书人

急如何为联合的两个不同的表查询结果集

发布时间: 2012-04-04 16:38:51 作者: rapoo

急:怎么为联合的两个不同的表查询结果集创建唯一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 table1union   select id,title from table2) td)tewhere 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 table1union   select id,title from table2) td
[解决办法]
2000用identity

读书人网 >SQL Server

热点推荐