2011-04-03 60 views
1

我想更新mysql數據庫,其中directory = 0,只是更新5 of records其中值0artphp mysql更新限制

的解釋:

id | directory 
1  | fashion 
2  | 0 //update here into 'art' 
3  | travel 
4  | fashion 
5  | 0 //update here into 'art' 
6  | 0 //update here into 'art' 
7  | travel 
8  | 0 //update here into 'art' 
9  | 0 //update here into 'art' 
10 | 0 //this is 6th record, do not update, leave the value as '0'. 
11 | fashion 

這是更新的代碼嗎?謝謝。

mysql_query("UPDATE articles SET directory = 'art' WHERE directory ='0' LIMIT 5"); 
+0

是查詢看起來不錯... – 2011-04-03 10:07:23

回答

4

您的syntax沒問題。

我會BY子句添加訂單(以確保)

ORDER BY `Id` 

查詢

UPDATE articles SET directory = 'art' WHERE directory ='0' ORDER BY id LIMIT 5 
1

似乎你的查詢沒有我錯了。

但請注意,你可能還需要指定一個order by條款,以確保它們是五個「第一」項目:

update articles 
set directory = 'art' 
where directory = '0' 
order by id 
limit 5 


只是作爲參考:UPDATE Syntax