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
https://forums.phpfreaks.com/topic/117992-insert-set-column-maxcolumn-1/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.