0

我有以下过程说明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) 给出此错误的行.我该如何解决它。最小值和最大值的值返回正常。

4

1 回答 1

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

也读这个

于 2012-09-05T16:45:47.060 回答