有两个表,一个是“信息表” 一个是“附加条件表”,这个sql语句带有分页功能的。原来的功能是从信息表里查信息,附加条件表里存有id的显示在前面。set @temp='select top '+str(@page_size)+' * from 信息表 where '+@str_where+' order by case when exists(select 1 from 附加条件表 where 信息表.id=id) then (select top 1 附加条件表排序字段 from 附加条件表 where 信息表.id=id) end desc,'+@str_order_by
现在想把  “附加条件表”的“附加条件表排序字段”这一列也显示出来,应该怎么办? @str_where  @str_order_by 要做怎样的改动?

解决方案 »

  1.   

    'select top '+str(@page_size) a.*,(select top 1 附加条件表排序字段 from 附加条件表 where id=a.id) as 附加条件表排序字段 from 信息表 a where '+ @str_where +' order by (select top 1 附加条件表排序字段 from 附加条件表 where id=a.id) desc, '+@str_order_by
      

  2.   

    测试了一下,这样也行:
    'select top '+str(@page_size) a.*,(select top 1 附加条件表排序字段 from 附加条件表 where id=a.id) as 附加条件表排序字段 from 信息表 a where '+ @str_where +' order by 附加条件表排序字段 desc, '+@str_order_by
      

  3.   

    谢谢qianjin036a ,通过是通过了,但是似乎结果有点问题,我有3个符合条件的结果,我在客户端读取值只读到2个,而且值还是错的,不知道为什么
      

  4.   

    我是这么写的我原来的
    set @temp='select top '+str(@page_size)+' * from 信息表 where '+@str_where+' order by case when exists(select 1 from 附加条件表 where 信息表.id=id) then (select top 1 附加条件表排序字段 from 附加条件表 where 信息表.id=id) end desc,'+@str_order_by
    你的
    'select top '+str(@page_size) a.*,(select top 1 附加条件表排序字段 from 附加条件表 where id=a.id) as 附加条件表排序字段 from 信息表 a where '+ @str_where +' order by 附加条件表排序字段 desc, '+@str_order_by
    按你的修改的
    set @temp='select top '+str(@page_size)+' a.*,(select top 1 附加条件表排序字段 from 附加条件表 where id=a.id) as 附加条件表排序字段 from 信息表 as a where '+@str_where+' order by case when exists(select 1 from 附加条件表 where 信息表.id=id) then (select top 1 附加条件表排序字段 from 附加条件表 where 信息表.id=id) end desc,'+@str_order_by
      

  5.   

    qianjin036a 
    非常感谢!就是按你的方法!谢谢!