Pioden Posted April 11, 2008 Share Posted April 11, 2008 I want to increment some values in a MySQL (5) db. The basic idea is where values in column X are greater than $somenumber the values in column X will be incremented by 1 ... For example if $somenumber = 5 all the values in column X which are greater than 5 have one added to them. I'm pretty certain I can do this with a series of queries and PHP processing but that feels like a horribly clunky way of doing it. Is there an SQL query/command which will help? I was thinking along the lines of: SQL = "UPDATE table SET column_x +1 WHERE column_x >5" ; Is this possible? My MySQL bible doesn't say much about this (or I'm looking in the wrong place!). TIA Huw Quote Link to comment Share on other sites More sharing options...
friedemann_bach Posted April 11, 2008 Share Posted April 11, 2008 This works, though your query should read: UPDATE table SET number = number+1 WHERE number > 5 Quote Link to comment Share on other sites More sharing options...
Pioden Posted April 14, 2008 Author Share Posted April 14, 2008 Thanks for that. Not quite getting it though :-( I wrote this - which looks right to me but throws a syntax error $sql = "UPDATE navigation SET (pageorder +1) WHERE pageorder >='$newpageorder' AND page_uid !='$sent_page_uid' "; Any ideas where I went wrong? It looks right to me!! ??? Quote Link to comment Share on other sites More sharing options...
fenway Posted April 14, 2008 Share Posted April 14, 2008 What's the syntax error? Can you echo $sql? Quote Link to comment Share on other sites More sharing options...
Pioden Posted April 14, 2008 Author Share Posted April 14, 2008 Got the answer from a friend. $sql = "UPDATE navigation SET pageorder = pageorder +1 WHERE pageorder >='$newpageorder' AND page_uid !='$sent_page_uid' "; Works nice now. Quote Link to comment Share on other sites More sharing options...
fenway Posted April 14, 2008 Share Posted April 14, 2008 Got the answer from a friend. Works nice now. Next time, consider asking your friend BEFORE you post... otherwise, you're wasting everyone's time. Quote Link to comment 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.