Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

November 3, 2009

MySQL INSERT with a Max()+1 Subquery

Filed under: The Coding World — admin @ 4:47 pm

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 you want to increase is not the primary key.

View Code MYSQL
INSERT INTO table (row1, row2, row3)
    SELECT 1 + COALESCE((SELECT MAX(row1) FROM table), 0), 'value2', 'value3'

2 Responses to “MySQL INSERT with a Max()+1 Subquery”

  1. Paul says:

    Thank you! I wasted an hour trying to do this before finding your site, sorted me out! Cheers

  2. juzek says:

    lovely ! :)

Leave a Reply

© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.