Jump to content
xisto Community
mizako

Increment A Mysql Column how to increment a MySQL column one unit

Recommended Posts

Hi,I have a column in a MySQL table which contains a counter of the views of the object described by this table. I would like to increment this value by one everytime the object is viewed. Obviously came into my mind the possibility of retrieving the value of this field, store it in a variable increment this value by one and perform an UPDATE query again with this new value. My question is if there is a MySQL option to update the field with its actual value plus an unit increment. I hope you understand the issue.

Share this post


Link to post
Share on other sites

What you are trying to achieve can be done in a single SQL statement without the need to return any records... In fact, 1 simple update statement like:

UPDATE MyTable SET MyColumn=MyColumn+1 Where MyID=123

Put that in a string and pass it to MySQL... I hope that helps!

Share this post


Link to post
Share on other sites

I believe you can easily do that by going to your phpmyadmin and configurate the auto-increment fields and it'll make it a column there itself...

Share this post


Link to post
Share on other sites

Auto-increment only works for insert statements, as it gives the next row a new ID number, but all previous rows will remain unaffected.He wants to update an existing row (via code) and increment any one of the columns in it, thus the simple update statement I posted above would work fine for this purpose...

Share this post


Link to post
Share on other sites

What you are trying to achieve can be done in a single SQL statement without the need to return any records...  In fact, 1 simple update statement like:

 

UPDATE MyTable SET MyColumn=MyColumn+1 Where MyID=123

Put that in a string and pass it to MySQL...  I hope that helps!

162207[/snapback]


Thanks that was the solution i was looking for!!

Share this post


Link to post
Share on other sites

can i display upcoming row id in my form and can later fill all the information

Increment A Mysql Column

 

Hello all,

 

I want to display the upcoming record no which is autoincrement and when I open add form for fill data, id of that row I want to display and later I want to fill rest of the record. So is there any function in php so I can show this.

 

-question by Yogendra Mishra

Share this post


Link to post
Share on other sites

auto incement

Increment A Mysql Column

 

How can I implement autoincrement in mysql?

Please explain with one example.

 

-question by RamManohar Nukala

 

------- admin reply --------

 

You have to set the property of column as AUTO-INCREMENT. And then feel NULL to it, it will take the NEXT value.

 

-Shree (Xisto.com)

Share this post


Link to post
Share on other sites
Increment A Mysql Column - how to increment a MySQL column with some input value.Increment A Mysql Column

hi,

I want to increment my mysql column with an input 'x'.

Wil UPDATE EMPLOYEE SET points=points+x  be a solution?

My doubt is wheather I can increment the column directly using the variable x or need to use '%d' (or %s ?...Total confusion..!!)?

what should be tha datatype of x?

Also

sprintf(Query,"INSERT INTO category(ID,Cat,CatID) VALUES(NULL,'%s',%d)", newCatName, atoi(newCatID));

here,when I want to display the CatID field, a '%s' ( taking it as a string) wil do?

Since I am trying to learn through websites,I want somebodies help.Please Help.

Thank You Very Much.

-question by Aswathy

Share this post


Link to post
Share on other sites

I was looking for this - hope it helps somebody - works fine in mysql

SET @pos =0;UPDATE players SET row_id = ( SELECT @pos := @pos +1 );

-reply by alextoader

 

Share this post


Link to post
Share on other sites
my answerIncrement A Mysql Column

I was looking for this - hope it helps somebody - works fine in mysql

SET @pos =0;UPDATE players SET row_id = ( SELECT @pos := @pos +1

-reply by alextoader

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.