Digital Media, Web Design, and Web Development by Ethan Kent | ethanmultimedia.com

ethan -at- ethanmultimedia -dot- com
six one nine - two four six - four one two zero

How To Select An Incrementing Column With MySQL

Introduction

This comes in handy when performing a mass insert from a table without a primary key to a table with an AUTO_INCREMENT value. A common tecnique simply inserts blank values to the AUTO_INCREMENT column. Doing so generates a warning for each row. Generating a bunch of warnings makes it difficult to identify and address the other warnings that may occur. Using my tecnique to generate incremental values addresses this problem.

Initialize the variable and set the starting value.

SET @INC = 15;

Put the variable into the select statement with a := operator.

SELECT
   @INC:=@INC+1 AS 'Record Number', 
   `column1`, 
   `column2`, 
   `etc`
FROM 
   `table1`;

Filed Under: ,


Comment

Textile Help

Previous Article

Next Article

About This Site

The contents, design, and development of this site are all done by me, Ethan Kent.

The contents of my site, ethanmultimedia.com, are for general information only and provided strictly as is. I do my best to be truthful and accurate, but I must expressly disclaim any implied warranties of non-infringement, merchantability and fitness for a particular purpose. Browse this site at your own risk. If you’re looking for safety and assurance you should probably seek the advice of an appropriate professional.

Contact Digital Media and Web Designer Ethan Kent - based in San Diego, CA.