php mysql update limit

淺唱寂寞╮ 提交于 2019-12-02 07:51:10

问题


I want to update mysql database, where directory = 0, and just update 5 of records which value 0 to art.

for explain:

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

Is this update code right? thanks.

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

回答1:


your syntax is fine.

i will add an order by clause (to be sure)

ORDER BY `Id`

to query

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



回答2:


Your query doesn't seem wrong to me.

But note that you might also want to specify an order by clause, to be sure which are the five "first" items :

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


Just as a reference : UPDATE Syntax



来源:https://stackoverflow.com/questions/5528885/php-mysql-update-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!