无法重新打开表mysql

时间:2012-09-05 15:53:45

标签: mysql stored-procedures

我有以下程序说can't reopen table为什么会出现此错误。这是查询:

DECLARE rangee INT;
DECLARE uid BIGINT;

SET @rangee = plimitRange * 10; 
SET @uid    = puserid;

DROP TEMPORARY TABLE IF EXISTS Rangee;
CREATE TEMPORARY TABLE Rangee(max BIGINT,min BIGINT);

PREPARE STMT FROM
'INSERT INTO Rangee
select max(postid),MIN(postid) from
(
select wall.postid from wall,posts where  
wall.postid = posts.postid and posts.userid=?
order by wall.postid desc LIMIT 10 OFFSET ?
)m;
';

 EXECUTE STMT USING @uid,@rangee;
 DEALLOCATE PREPARE STMT;


 select comments.comment,comments.postid,user.name,comments.userid 
 from user,posts,comments where 
 posts.postID = comments.postid and 
 comments.postid<=(select max from Rangee) and 
 comments.postid>=(select min from Rangee) and posts.userid = puserid and 
 user.userid=comments.userid order by comments.postid desc;

这里我将值minmax id's插入到另一个表的临时表中,以便我可以使用这些值在最终查询中检索我的数据。但是在最后的查询中我在哪里我指定了范围,即包含(select max from Rangee)(select min from Rangee)的行给出了这个错误。我怎么解决呢.min和max的值都很好。

1 个答案:

答案 0 :(得分:0)

<UPDATE>

然后忘记整个过程,在一个查询中执行:

select comments.comment,comments.postid,user.name,comments.userid 
 from 
 user
 INNER JOIN comments ON user.userid=comments.userid
 INNER JOIN posts ON posts.postID = comments.postid
 WHERE 
 comments.postid <=  
 (select MAX(postid) from
    (
    select wall.postid from wall,posts where  
    wall.postid = posts.postid and posts.userid=?
    order by wall.postid desc LIMIT 10 OFFSET ?
    )sq1
 )

 and 
 comments.postid >= 
 (select MIN(postid) from
    (
    select wall.postid from wall,posts where  
    wall.postid = posts.postid and posts.userid=?
    order by wall.postid desc LIMIT 10 OFFSET ?
    )sq2
 ) 
 and 
 posts.userid = puserid 
 order by comments.postid desc;

就是这样。

</UPDATE>
老答案......

DECLARE rangee INT;
DECLARE uid BIGINT;

SET @rangee = plimitRange * 10; 
SET @uid    = puserid;

PREPARE STMT FROM
'select @max_postid := MAX(postid), @min_postid := MIN(postid) from
(
select wall.postid from wall,posts where  
wall.postid = posts.postid and posts.userid=?
order by wall.postid desc LIMIT 10 OFFSET ?
)m;
';

 EXECUTE STMT USING @uid,@rangee;
 DEALLOCATE PREPARE STMT;


 select comments.comment,comments.postid,user.name,comments.userid 
 from user,posts,comments where 
 posts.postID = comments.postid and 
 comments.postid<=@max_postid and 
 comments.postid>=@min_postid and posts.userid = puserid and 
 user.userid=comments.userid order by comments.postid desc;

根本不需要临时表。同样做comments.postid <= (select max from Rangee)也不好,因为这可能会返回多行。您应该至少使用comments.postid <= (select MAX(max) from Rangee)

另请阅读this