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

使用sqlserver查询两表,并实现分页,代码如下
select top 7 users.*,users.u_id as usersid,
  users.*,company.u_id as companyid ,company.* 
   
  from t_users users,t_company company 
  where users.u_id=company.u_id 
  order by users.u_id desc
   

  这样是正确的




select top 5 allinfo.* from 
(
  select top 7 users.*,users.u_id as usersid,
  users.*,company.u_id as companyid ,company.* 
  from t_users users,t_company company 
  where users.u_id=company.u_id 
  order by users.u_id desc

 allinfo


使用下面的语句后,提示allinfo 多次引用 u_id 网前辈给看一下,sqlserver的语句

------解决方案--------------------
select top 5 allinfo.* from
(
select top 7 users.*,users.u_id as users_u_id ,
users.*,company.u_id as company_u_id ,company.*
from t_users users,t_company company
where users.u_id=company.u_id
order by users.u_id desc
)
 allinfo

------解决方案--------------------
users.*,company.* 都含有u_id ,在allinfo中重复了

一一列出两表的字段,重复的字段改名应该可以