Re: MySQL Auto PK

From: Date: Thu, 06 Jan 2005 18:29:45 +0000
Subject: Re: MySQL Auto PK
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-38022@lists.php.net to get a copy of this message
On Wed, 05 Jan 2005 18:11:23 -0500, John Holmes <holmes072000@charter.net> wrote: > OOzy Pal wrote: > > Is it possible to have mysql at an ID as 20050105-1 as > > (YYYYMMDD-1), -2, etc. > > automatically? No. But you can always just use > > SELECT CONCAT(date_column,'-',pk_column) AS fixed_id ... > > if you _really_ need something like this. Or just join them together in > PHP. I think the only downside to that solution is that the primary key will continue to increment regardless of the day, and I think the original poster wanted: 20050105-1 20050105-2 20050106-1 ...etc. This would be a great place for a stored procedure, but I don't know if I can recommend running MySQL 5 to you. The most platform-safe way I can think of is to get a count(*) of the number of rows with today's date, add 1 to it, and stick that number on the end of the string you've created to insert into a varchar field. It's an extra query per insert, but it'd do the job.

« previous php.db (#38022) next »