DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

MySQL Insert Unique Reference

09.25.2009
| 2034 views |
  • submit to reddit
        // MySQL Insert Unique Reference using MAX() to obtain latest value
// Alternate solution to using Composite Primary Keys which INNODB engine does not support.

INSERT INTO jobs (companyid,clientref,jobid) VALUES 
(8,2,IFNULL((SELECT max(b.jobid)+1 FROM jobs b where b.clientref=2),1) );