2011-12-07 211 views
0

我有一個更新查詢列表,但想知道是否有更快或更好的方式做到這一點,也許有某種循環。多個SQL更新查詢

mysql_query("UPDATE turfs SET 1 = '0' WHERE 1 = '$corpse_gang'"); 
mysql_query("UPDATE turfs SET 2 = '0' WHERE 2 = '$corpse_gang'"); 
mysql_query("UPDATE turfs SET 3 = '0' WHERE 3 = '$corpse_gang'"); 
mysql_query("UPDATE turfs SET 4 = '0' WHERE 4 = '$corpse_gang'"); 
mysql_query("UPDATE turfs SET 5 = '0' WHERE 5 = '$corpse_gang'"); 

mysql_query("UPDATE turfs SET color1 = '0' WHERE color1 = '$g_color'"); 
mysql_query("UPDATE turfs SET color2 = '0' WHERE color2 = '$g_color'"); 
mysql_query("UPDATE turfs SET color3 = '0' WHERE color3 = '$g_color'"); 
mysql_query("UPDATE turfs SET color4 = '0' WHERE color4 = '$g_color'"); 
mysql_query("UPDATE turfs SET color5 = '0' WHERE color5 = '$g_color'"); 

回答

1

是的,我認爲這將是這樣的:

for ($i = 1; $i <= 5; $i++) { 
    mysql_query("UPDATE turfs SET " . $i . " = '0' WHERE " . $i . "= '$corpse_gang'"); 
    mysql_query("UPDATE turfs SET color" . $i . " = '0' WHERE color" .$i . " = '$g_color'"); 
}