一个带子查询的update语句

张海霖 2011-05-11 10:33:13
update pw_threads a inner join (select *
from pw_threads A
where 5>(select count(*) from pw_threads where A.fid=fid and A.postdate<postdate))
b set a.lastpost=(select lastpost from (select lastpost from pw_threads order by lastpost desc limit 0,1) a)+1

这段语句并没有语法错误,但是没有达到只更新子查询中限定的范围的作用,把整个pw_theads的所有记录全部都更新了
请问应该怎么改一下。
...全文
290 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
张海霖 2011-05-11
  • 打赏
  • 举报
回复
update pw_threads a inner join (select *
from pw_threads A
where 5>(select count(*) from pw_threads where A.fid=fid and A.lastpost>lastpost))
b on a.tid=b.tid set a.lastpost=(select lastpost from (select lastpost from pw_threads order by lastpost desc limit 0,1) a)+1
这样可以了,似乎行了,请高手看看有什么可以优化一下的地方吗,
语句是不是太长了点
张海霖 2011-05-11
  • 打赏
  • 举报
回复
tid就是自增列
张海霖 2011-05-11
  • 打赏
  • 举报
回复
没有ON。应该有吗
update pw_threads a inner join (select *
from pw_threads A
where 5>(select count(*) from pw_threads where A.fid=fid and A.lastpost>lastpost))
b set a.lastpost=(select lastpost from (select lastpost from pw_threads order by lastpost desc limit 0,1) a)+1 on a.tid=b.tid
这样改语法错误
wwwwb 2011-05-11
  • 打赏
  • 举报
回复
你的ON条件在什么地方
张海霖 2011-05-11
  • 打赏
  • 举报
回复
子查询那里不应该是postdate,而应该是lastpost
select *
from pw_threads A
where 5>(select count(*) from pw_threads where A.fid=fid and A.lastpost>lastpost)
order by fid

修正一下

update pw_threads a inner join (select *
from pw_threads A
where 5>(select count(*) from pw_threads where A.fid=fid and A.lastpost<lastpost))
b set a.lastpost=(select lastpost from (select lastpost from pw_threads order by lastpost desc limit 0,1) a)+1

但是还是更新所有表中的记录,不是只更新每个板块最老的5条
张海霖 2011-05-11
  • 打赏
  • 举报
回复
目的是找出所有板块最老的5个帖子,把他们的最后回复时间更新成最新,(也就是把最老的帖子提前到最前面)

我的方法是找出所有帖子的最后更新的那个lastpost,也就是最前面的一个帖子。
用这个时间+1就是比最新的帖子还新。

把最老的帖子的最后回复时间设置成比最新的帖子还新。

我先取出每个板块最老的5个帖子,进行更新。

pw_threads是phpwind论坛的主贴表,里面有id是自增字段,fid是版块id,lastpost是最后回复时间,
postdate是发帖时间,似乎我个语句里面不应该有postdate,和我要实现的目的无关
wwwwb 2011-05-11
  • 打赏
  • 举报
回复
要达到什么目的?举例说明
张海霖 2011-05-11
  • 打赏
  • 举报
回复
有where 了,干嘛再弄一个where,而且要放在where中的就是子查询,因为mysql不支持在where中有子查询所以才放到inner join那里。

我发现我有两个表使用了别名a,把后一个改成了别名c,还是一样,不是别名问题
rucypli 2011-05-11
  • 打赏
  • 举报
回复
update pw_threads a inner join 
(select * from pw_threads A where 5>(select count(*) from pw_threads where A.fid=fid and A.postdate<postdate)) b
set a.lastpost=(select lastpost from (select lastpost from pw_threads order by lastpost desc limit 0,1) a)+1
where .....

57,062

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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