0

次のクエリがあります。

select wall.postid from wall,posts where  
wall.postid = posts.postid and posts.userid=puserid
order by wall.postid desc LIMIT 4 OFFSET 0;

次の方法で結果を返します。

  wall.postid
-----------------
      52
      51
      50
      49

ここで、次のクエリで使用できるように、最大​​値、つまり 52 と最小値、つまり 49 を変数に保存したいと考えています。次のクエリを使用してそうしています。

select @upper:=max(wall.postid),@lower:=min(wall.postid) from wall,posts where  
wall.postid = posts.postid and posts.userid=puserid
order by wall.postid desc LIMIT 4 OFFSET 0;

私は持っていますが、これらの変数は、この結果セットではなく、列の最大 ID と最小 ID を保存します。つまり、列の最小値である最大 = 52 と最小 = 41 を返します。最小 = 49 が必要です。

4

2 に答える 2

2

必要な結果を得るには、集計の前に制限を行う必要があります

 select
     @upper = max(postID),
     @lower = min(postID)
 from
 (
      select wall.postid from wall 
          inner join posts on wall.postid = posts.postid 
      where   
      posts.userid=puserid 
      order by wall.postid desc LIMIT 4 OFFSET 0
 ) as v

改善された ANSI-92 結合構文にも注意してください。

于 2012-09-03T12:25:36.990 に答える
0
select @upper:=max(`postid`), @lower:=MIN(`postid`) 
from
(
select wall.postid from wall,posts where  
wall.postid = posts.postid and posts.userid=puserid
order by wall.postid desc LIMIT 4 OFFSET 0)m
于 2012-09-03T12:27:21.320 に答える