$sql = "UPDATE purchase SET stat='1' WHERE `id`='$id' AND 4*(plan)<= sum(plan) ";
或
$sql = "UPDATE purchase SET stat='1' WHERE `id`='$id' AND 4*(plan-1)<= sum(plan-1) ";
我æ£åœ¨å°è¯•é€šè¿‡æ¤æ›´æ–°è¿›è¡ŒæŸ¥è¯¢ï¼Œå¦‚果所有行的总和大于第一行,它将更新第一行,并且æ¤è§„则适用于所有行。但是它显示错误并说
Error updating record: Invalid use of group function
æ¤æŸ¥è¯¢æœ‰ä»€ä¹ˆé—®é¢˜ï¼Ÿæˆ‘需è¦æ¤é”™è¯¯çš„帮助。
ç”案 0 :(得分:1)
您ä¸èƒ½åœ¨æŸ¥è¯¢çš„SUM()
åå¥ä¸ä½¿ç”¨åƒWHERE
è¿™æ ·çš„èšåˆå‡½æ•°ï¼Œå› 为SUM()
是在所有选定行上计算的,而WHERE
是用于选择行。您需è¦åœ¨å•ç‹¬çš„查询ä¸è®¡ç®—总和,然åŽå°†å…¶åŠ 入。
UPDATE purchase
JOIN (SELECT SUM(plan) AS plantotal FROM purchase) AS p1
ON 4*(plan-1) <= plantotal
SET stat = '1'
WHERE id = '$id'
è¦ä½¿æ›´æ–°åŸºäºŽå…·æœ‰æ›´é«˜ID的所有行,您å¯ä»¥ä¸Žä¸€ä¸ªå查询结åˆä½¿ç”¨ï¼Œè¯¥å查询获å–æ¯ç»„行的总数。
UPDATE purchase AS p
JOIN (SELECT p1.id AS id, SUM(p2.plan) AS plantotal
FROM purchase AS p1
JOIN purchase AS p2 ON p2.id > p1.id) AS p3
ON p3.id = p.id AND 4*(p.plan-1) <= p3.plantotal
SET stat = '1'