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 Link to comment https://forums.phpfreaks.com/topic/100616-solved-mysql-query-question/ 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 Link to comment https://forums.phpfreaks.com/topic/100616-solved-mysql-query-question/#findComment-514581 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!! ??? Link to comment https://forums.phpfreaks.com/topic/100616-solved-mysql-query-question/#findComment-516636 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? Link to comment https://forums.phpfreaks.com/topic/100616-solved-mysql-query-question/#findComment-516724 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. Link to comment https://forums.phpfreaks.com/topic/100616-solved-mysql-query-question/#findComment-516764 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. Link to comment https://forums.phpfreaks.com/topic/100616-solved-mysql-query-question/#findComment-516768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.