Jump to content

(INSERT) SET column = MAX(column) + 1


xtopolis

Recommended Posts

Hi,

I wanted to know if I can update a column value to equal the max of the column + 1 (like auto increment, but not the same)

 

INSERT INTO table SET pos = MAX(pos) + 1  // is what I would logically want to do, but it gives me invalid use of group function

 

Do I need to do a "SELECT MAX(pos) as curpos FROM table";  And then use php to ++1 on it, then do ($curpos++);

"INSERT INTO table SET pos = '$curpos'"

 

or can I do it as a one liner, per say.

 

 

simple structure, example data:

[id][pos][name]      id is auto_inc, pos[int,unique], name[varchar255]

 

[3][1][File 3]

[1][2][file 1]

[5][3][banana]

[9][4][data]

[4][5][file]

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.