如何选择多行,直到列达到数字限制

时间:2016-04-03 20:50:45

标签: php mysql

我想知道的是,在列值达到特定数字之前,我将如何设置查询以选择表并仅选择一定量的行。我会尝试用代码更好地解释它:

$get_armory = mysql_query("
SELECT * 
  FROM armory_owned 
 WHERE owner_id=$id 
   AND owned <= $mob_size 
 ORDER 
    BY attack");

因此,如果有人拥有250个Mob大小,他们可以在攻击期间使用250个暴徒大小。

示例:他们拥有400个匕首,其中有2个攻击,100个剑,5攻击。我如何设置查询以检索最好的武器,如100剑和150把匕首等于总共250件武器?

1 个答案:

答案 0 :(得分:0)

使用它:

$get_weapons = mysql_query("SELECT 
   O.player_id,
   O.item_id,
   O.attack,
   O.owned,
    (SELECT 
      sum(owned) FROM items_owned 
    WHERE owned <= O.owned AND player_id=$id) 'RunningTotal' 
FROM items_owned O
HAVING RunningTotal <= $mob_avail 
ORDER BY attack DESC");