为含糊的标题道歉
鉴于以下数据..
+------+------------+--------+
| 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'")
}
所以,很丑。更好的方法的建议?