使用union联合查询时分页时数据显示不正确

rendehua380 2010-06-12 11:06:10
我的sql语句是这样的

select top 1 * from (select cId,cName,cNumber,cAddress,cProducts,case when cId>0 then '客户' end as Type,case when cId!=0 then cId+1 end as id from Customers where cUId=1
union select pId,pName,pNumber,pAddress,pProducts,case when pId>0 then '供应商' end as Type,case when pId>0 then pId+2 end as id from Providers where pUId=1
union select hId,hName,hNumber,hAddress,hProducts,case when hId>0 then '潜在客户' end as Type,case when hId>0 then hId+3 end as id from HideCustomers where hUId=1
)Customers where Customers.cId not in (select top 0 cId from(select cId from Customers where cUId=1 union select pId from Providers where pUId=1 union select hId from HideCustomers where hUId=1)Customers order by Customers.cId) order by Customers.cId


错误的地方是分页的时候显示数据不正确,我是根据主键ID来分页的,数据中主键ID有肯能重复
请教各位大侠如何解决这个问题
...全文
254 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2010-06-12
  • 打赏
  • 举报
回复
select top 0 cId ??
caixia615 2010-06-12
  • 打赏
  • 举报
回复
重复主键ID用作分页不行吧,另外换个唯一键字段吧

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧