Matthew Oatham wrote:

I have a MySQL table which uses a auto_increment int as the primary key - when I do an insert I want to create another id based on the primary key and insert this into the same table i.e. if I do an insert and the primary key is 3 I want to generate the ID CR-003, if the primary key was 34 I would want to generate the id CR-034. So I guess I need to know the following.

You don't want or need to do this. You're just repeating data. The "34" is already there in one column, why repeat it (especially if the "CR" bit never changes).


Just select out when you need in the query or use PHP to make the combination.

SELECT CONCAT('CR-',id) AS formatted_number FROM table WHERE ...

Or use PHP, where $id is the primary key

$formatted_number = 'CR-' . $id;

You can use a ZEROFILL attribute on your column to ensure the leading zeros get returned or use sprintf() in PHP to format the number.

Even if the "CR" changes, then just put that bit in another column and join them together later.

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to