This was a pain in my ass to figure out so I figured I would post it here in case anyone else needs to insert a record using a Max() + 1 SubQuery.
I only use the Max()+1 SubQuery when the value I want to increase is not the primary key.
INSERT INTO table (row1, row2, row3) SELECT 1 + COALESCE((SELECT MAX(row1) FROM table), 0), 'value2', 'value3';
Comments
Thank You
Thank you! I wasted an hour trying to do this before finding your site, sorted me out! Cheers
lovely!
lovely! :)
Thanks
Great article thank you very much, you just saved from same kind of pain..:)Thank you !Cheers from Belgrade
Excellent!
MySQL really needs to document this as an alternative. Excellent work.
It works !
Thanks a lot ! Simple and efficient ! Just what I wanted !
thanks
Hi, thanks a lot for the query. Saved me some time a nervs.greetings from Czech Republic ;-)