當在PHP中使用SQL時,能夠將SQL放在循環語句中。但這麼作給你的數據庫增長了負擔。php
下面的例子說明了「在循環語句中嵌套查詢語句」的問題:mysql
foreach ($display_order as $id => $ordinal){ $sql = "UPDATE categories SET display_order = $ordinal WHERE id = $id"; mysql_query($sql); }
你能夠這麼作:web
UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHERE id IN (1,2,3)詳細介紹: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query