Generate number depending on last insert!??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Generate number depending on last insert!??

Post by jmansa »

I'm trying to generate a new number/next number depending on the last number inserted into my db???

In my DB I have a column where the numbers look like this:

00001.00001
00001.00002
00001.00003
00002
00003.00001
00004
etc. etc.


This is done for pages to find its place in a hierarchy.

My question is as follows:
Is it possible to make a script that looks into the dbtable and finds out what the last number in ex. 00001.xxxxx, and then generates the next comming number? And if yes... How.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could use MAX() if it was only numbers (I don't believe MAX() works with strings, although it might), but instead, you could order the selections starting with the latest, and then select the first of that set (which is the latest entry).

Code: Select all

select from `table` order by `column` desc limit 1;
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post by jmansa »

I dont think that would work, caurse im only looking for making this work if the prevous record has the 00001.xxxxx and not 00002, 00003 and so on!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Sounds like you're in need of a better database design.

Also, maybe MySQL's SUBSTR() could be of use.

Code: Select all

SELECT FROM `table` WHERE SUBSTR(`column`, 0, 6) = '00001.' ORDER BY `column` DESC LIMIT 1;
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

That data needs more normalization. "category" might work better as a separate column.

Search for "has many" and "has and belongs to many" for a quick overview of record relationships.
Post Reply