请帮忙修改一条sql语句

kcsg 2007-10-17 11:21:46
sql="select top 5 b.id,b.cpmc,b.dateandtime,b.gsid,b.zsbz from (select max(id) id from spzs where flag=1 group by gsid) a inner join spzs b on a.id=b.id left join hot_top c on b.gsid=c.gsid order by case when c.gsid is not null then 0 when b.zsbz like '%02%' then 1 else 2 end,b.dateandtime desc"

说明:取hot_top中存在的gsid的信息,不足的取其他信息,总共5条,条件是取日期最新的,但select max(id) id from spzs where flag=1 group by gsid只能取到id最大的,不能保证日期是最新的,因为有可能用户修改其他信息
...全文
105 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kcsg 2007-10-17
  • 打赏
  • 举报
回复
总共取5条信息,hot_top里设置了gsid,可能是多个gsid,优先取hot_top表中gsid的日期最新的一条信息,不重复,不足5条的,再取其他gsid的不重复的一条信息,凑足5条就可以了
kcsg 2007-10-17
  • 打赏
  • 举报
回复
总共取5条信息,hot_top里设置了gsid,优先取hot_top表中gsid的日期最新的一条信息,不重复,不足5条的,再取其他gsid的不重复的一条信息,凑足5条就可以了
Generics 2007-10-17
  • 打赏
  • 举报
回复
sql = 'select top 5 b.id,b.cpmc,b.dateandtime,b.gsid,b.zsbz 
from spzs b
left join hot_top c on b.gsid=c.gsid
order by case when c.gsid is not null then 0 when b.zsbz like ''%02% '' then 1 else 2 end, b.dateandtime desc'


Generics 2007-10-17
  • 打赏
  • 举报
回复
不明白你的表a是干啥的? 去掉不是更好么?


sql = 'select top 5 b.id,b.cpmc,b.dateandtime,b.gsid,b.zsbz from spzs b
left join hot_top c on b.gsid=c.gsid
order by case when c.gsid is not null then 0 when b.zsbz like '%02% ' then 1 else 2 end, b.dateandtime desc'


34,870

社区成员

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

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