哪位好心人帮我这个sql语句从一个表变成2个表,在线等,谢谢啦

colorrabbit 2008-12-22 07:50:34
有两个表,一个是“信息表” 一个是“附加条件表”,这个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 要做怎样的改动?
...全文
83 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
colorrabbit 2008-12-22
  • 打赏
  • 举报
回复
qianjin036a
非常感谢!就是按你的方法!谢谢!
colorrabbit 2008-12-22
  • 打赏
  • 举报
回复
我是这么写的

我原来的
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
colorrabbit 2008-12-22
  • 打赏
  • 举报
回复
谢谢qianjin036a ,通过是通过了,但是似乎结果有点问题,我有3个符合条件的结果,我在客户端读取值只读到2个,而且值还是错的,不知道为什么
-晴天 2008-12-22
  • 打赏
  • 举报
回复
测试了一下,这样也行:
'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
-晴天 2008-12-22
  • 打赏
  • 举报
回复
'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
colorrabbit 2008-12-22
  • 打赏
  • 举报
回复
等呀等!
ooily 2008-12-22
  • 打赏
  • 举报
回复
mark

34,576

社区成员

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

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