-1

为含糊的标题道歉

鉴于以下数据..

  +------+------------+--------+
  |  ID  |  value     | used   |
  +------+------------+--------+
  | 1001 |    1       |   0    |
  +------+------------+--------+
  | 1002 |    1       |   0    |
  +------+------------+--------+
  | 1003 |    2       |   0    |
  +------+------------+--------+
  | 1004 |    4       |   0    |
  +------+------------+--------+
  | 1005 |    1       |   0    |
  +------+------------+--------+
  | 1006 |    1       |   0    |
  +------+------------+--------+
  | 1007 |    1       |   0    |
  +------+------------+--------+
  | 1008 |    6       |   0    |
  +------+------------+--------+

我想设置 8 的总和值,所以结果将是....

  +------+------------+--------+
  |  ID  |  value     | used   |
  +------+------------+--------+
  | 1001 |    1       |   1    |
  +------+------------+--------+
  | 1002 |    1       |   1    |
  +------+------------+--------+
  | 1003 |    2       |   1    |
  +------+------------+--------+
  | 1004 |    4       |   1    |
  +------+------------+--------+
  | 1005 |    1       |   0    |
  +------+------------+--------+
  | 1006 |    1       |   0    |
  +------+------------+--------+
  | 1007 |    1       |   0    |
  +------+------------+--------+
  | 1008 |    6       |   0    |
  +------+------------+--------+

我会这样做,通过每个项目将其添加到总价值,类似于这个

  $goal = 8;
  $select = "SELECT ID, value FROM table WHERE !used";
  $result = mysql_query($select);
  while ($row = mysql_fetch_array($result))
     {
     $total = $total + $row['value'];
     if ($total => $goal) 
         { 
         $over = $goal - $total
         if ($over)
            {
            $id = $row['id'];
            mysql_query("INSERT INTO table(value,used) VALUES('$over',0)");         
            mysql_query("update table SET used = 1 WHERE id = '$id'")                 
            }  
          break;
         }
     $id = $row['id'];
     mysql_query("update table SET used = 1 WHERE id = '$id'")  
     }

所以,很丑。更好的方法的建议?

4

3 回答 3

1

试一试:

      $goal = 8;
      $used = Array()
      $select = "SELECT ID, value FROM table WHERE !used";
      $result = mysql_query($select);
      while ($row = mysql_fetch_array($result))
         {
         $total = $total + $row['value'];
         $used[] = $row['ID'];
         // Check for exact march
         if ($total == $goal) {
             // Loop through all used ID's to get an exact match
             foreach($used as $usedId) {
                mysql_query("update table SET used = 1 WHERE id = " . $usedId);                 
             }
             break;
         } elseif ($total > $goal) { // If over then insert as a new record
             $leftOver = $total - $goal;
             mysql_query("INSERT INTO table(value,used) VALUES($leftOver, 0)");         
             break;
         }
      }

这没有回答的是,如果总数低于您的目标 - 那么会发生什么?

于 2013-02-25T09:02:58.830 回答
0

试试这个查询 -

SELECT t1.ID, t1.value, IF(SUM(t2.value) > 8, 0, 1) used FROM used t1
  JOIN used t2
    ON t1.ID >= t2.ID
  GROUP BY t1.ID
于 2013-02-25T09:24:02.447 回答
0

首先,您不应该使用旧的和不推荐使用的mysql_*功能。使用MySQLiPDO代替。

$goal=13;
$cache=0;
$idCache=0;
$result=$mysqli->query("SELECT id,value FROM [table] WHERE used=0 ORDER BY id ASC");
while($row=$result->fetch_assoc())
{
    $idCache=intval($row["id"]);
    $cache+=intval($row["value"]);
    if($cache>=$goal) break;
}
$result->free();
$stmt=$mysqli->prepare("UPDATE [table] SET used=1 WHERE used=0 AND id<=?");
$stmt->bind_param("i",$idCache);
$stmt->execute();
$stmt->close();
if($cache>$goal)
{
    $stmt=$mysqli->prepare("INSERT INTO [table] (value,used) VALUES (?,0)";
    $cache=$goal-$cache;
    $stmt->bind_param("i",$cache);
    $stmt->execute();
    $stmt->close();
}

解释:

  1. 从您的演示数据中,我假设您想要获取按 id 升序排序的数据,所以我用一个ORDER BY子句查询它们;

    $mysqli->query部分)

  2. 将结果相加,$cache直到它不小于$goal;

    while部分)

  3. 由于我们以 id-ascending 顺序获取值,因此我们可以安全地标记所有不大于我们“使用”的最后一行的 ID;

    $mysqli->prepareif块)

  4. 如果总和大于目标,我们会将“溢出”值添加回表中,并used标记为0,正如您在代码中所暗示的那样。

    if块)

请注意,上面的代码没有考虑“如果没有足够的值来总结$goal”的情况。如果它可能是一个真实的案例,你将不得不处理它。

于 2013-02-25T09:40:07.607 回答