Glese Posted December 10, 2011 Share Posted December 10, 2011 I accidentally executed the following SQL query a moment ago: UPDATE con SET contribution = '$contribution', category = '$contribution_category', description = '$contribution_description' This updated the whole table (every single row), which taught me a lesson. I would like to know is there a way to limit the query to just one single row, to avoid an updating of the whole table on big projects? This happened in the development environment, so it is not a big deal and can be checked as lesson learned. Quite dangerous this one. Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/ Share on other sites More sharing options...
The Little Guy Posted December 10, 2011 Share Posted December 10, 2011 UPDATE con SET contribution = '$contribution', category = '$contribution_category', description = '$contribution_description' limit 1 Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296651 Share on other sites More sharing options...
Glese Posted December 10, 2011 Author Share Posted December 10, 2011 So it is without an equal sign? And I tried Limit = '1', which did not work for me. Thanks for the tip. Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296656 Share on other sites More sharing options...
mikosiko Posted December 11, 2011 Share Posted December 11, 2011 just a note... even when LIMIT will work you normally should use a WHERE clause to specify exactly the scope of your sentence Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296660 Share on other sites More sharing options...
The Little Guy Posted December 11, 2011 Share Posted December 11, 2011 So it is without an equal sign? yup, just like a select. Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296673 Share on other sites More sharing options...
fenway Posted December 11, 2011 Share Posted December 11, 2011 just a note... even when LIMIT will work you normally should use a WHERE clause to specify exactly the scope of your sentence And without ORDER BY, LIMIT is basically random. Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296684 Share on other sites More sharing options...
Glese Posted December 11, 2011 Author Share Posted December 11, 2011 fenway, what do you mean by that? Will it not work how I imagined it in the sense of the thread? Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296717 Share on other sites More sharing options...
trq Posted December 11, 2011 Share Posted December 11, 2011 You need to use a WHERE clause to tell mysql which row you want to update. While LIMIT will limit the update to a single row, without a WHERE clause you are still not defining which row should be updated. Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296722 Share on other sites More sharing options...
fenway Posted December 11, 2011 Share Posted December 11, 2011 fenway, what do you mean by that? Will it not work how I imagined it in the sense of the thread? I mean that you'll get an arbitrary row - the order of the rows examined by MySQL is totally arbitrary (in the sense that you can't guess what it will be). if you want a particular one of the rows updated, and not others, you'll need both ORDER BY and LIMIT. Quote Link to comment https://forums.phpfreaks.com/topic/252913-limit-to-only-one-row/#findComment-1296778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.