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的所有记录全部都更新了
请问应该怎么改一下。

解决方案 »

  1.   

    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 .....
      

  2.   

    有where 了,干嘛再弄一个where,而且要放在where中的就是子查询,因为mysql不支持在where中有子查询所以才放到inner join那里。我发现我有两个表使用了别名a,把后一个改成了别名c,还是一样,不是别名问题
      

  3.   

    目的是找出所有板块最老的5个帖子,把他们的最后回复时间更新成最新,(也就是把最老的帖子提前到最前面)我的方法是找出所有帖子的最后更新的那个lastpost,也就是最前面的一个帖子。
    用这个时间+1就是比最新的帖子还新。把最老的帖子的最后回复时间设置成比最新的帖子还新。我先取出每个板块最老的5个帖子,进行更新。pw_threads是phpwind论坛的主贴表,里面有id是自增字段,fid是版块id,lastpost是最后回复时间,
    postdate是发帖时间,似乎我个语句里面不应该有postdate,和我要实现的目的无关
      

  4.   

    子查询那里不应该是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条
      

  5.   

    没有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
    这样改语法错误
      

  6.   

    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
    这样可以了,似乎行了,请高手看看有什么可以优化一下的地方吗,
    语句是不是太长了点