无法重新打开表mysql
我执行了以下过程,该过程说明无法重新打开表为什么会出现此错误。以下是查询:无法重新打开表mysql,mysql,stored-procedures,Mysql,Stored Procedures,我执行了以下过程,该过程说明无法重新打开表为什么会出现此错误。以下是查询: 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 'I
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;
在这里,我在另一个表的临时表中插入值min和max id,以便在最终查询中使用这些值检索我的数据。但是在最终查询中,我指定了范围,即包含select max from range和select min from range的行出现了此错误。我如何解决此问题。的值min和max恢复得很好
<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;
你根本不需要临时的桌子。另外,做评论也是不好的。postid thnx的答案@tombom我已经试过了,效果很好,但问题是它会返回我不想要的两个结果。我只需要上一次查询的结果。prepare语句中的select将返回一个结果,然后我的最终查询将返回另一个结果。我明白了。更新了我的答案。不要担心,因为查询看起来很复杂。它不一定会跑得更慢或其他什么。MySQL的优化器做得很好,可以检测子查询何时重复,但差异很小。它给出了以下错误:每个派生表都必须有自己的别名。@MJ192现在应该可以工作了。子查询缺少一个别名,仅此而已。thnx很多。这几天来我一直在坚持。