根据总和(值)设置字段

时间:2013-02-25 08:44:51

标签: php mysql

道歉的标题

鉴于以下数据..

  +------+------------+--------+
  |  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'")  
     }

所以,非常难看。一个更好的方法的建议?

3 个答案:

答案 0 :(得分: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;
         }
      }

如果总数低于你的目标,那么它无法回答的是什么呢?

答案 1 :(得分: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

答案 2 :(得分: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->prepare阻止前if

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

    if区块)

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