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) 。どうすれば解決できますか。min と max の値は問題なく返ってきます。

4

1 に答える 1

0
<UPDATE>

次に、手順全体を忘れて、1 つのクエリで実行します。

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 に答える