Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 6th, 2004, 04:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default Impure numeric values passed to MAX()

I've a column in a table which has values such as:

'EV000450'
'EV000451'
'EV000452'

etc.

This is fine: the expression is always of the format '^EV[0-9]{6}$', with the numeric portion auto-incrementing for each new record. I'm assured that this will always be the case (I have no faith in this assertion, but let's assume the client is telling the truth, for the moment, shall we...).

I take it there will be no problem my determining what the new value for this field will be when INSERTing a new record, by simply asking what the MAX() value for the field is, and adding one to the numeric portion of the value that's returned? What I mean is, is that there's no aspects about how MySQL regards the maximum value of mixed alpha-numeric field (that matches the RegExp I've given above), that is likely to come around the corner and bite me later on. Assuming that:
A) as I say, the fields continue to be in this format,
and
B) we have a safe margin of time to go between now and the point when the value EV999999 is taken?

If anyone has come across this kind of thing before, it'd be much apreciated.

Dan
 
Old April 6th, 2004, 01:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As long as the integer portion of your string is always left-padded with zeros, you should not have a problem.

MySQL will assume MAX as the last item of an alphabetical sort. You can also think of this as the first item in the result set that's sorted by this field in DESC order.

An alpha numeric sort works using ASCII character values, not interpreted numeric values. Therefore, you must be cautious because this means that 'a' is GREATER THAN 'Z'.

The "EV" portion of the string will always, obviously, be of equal value when sorting. This means that the sort is completely dependent upon the values of the sequential digit characters following "EV". The ASCII values for these characters are sequential and increasing, just like the values for the digits these characters represent.


Long story short, no, you shouldn't have a problem -- as long as the format of the field does NOT change.
If you get to "EV999999" you'll have to deal with the wrapping case. ASCII places digit characters before letter characters, so you might be able to fudge your results by using letters for the higher numbers:

EV999999 + 1 = EV99999A

etc.


Take care,

Nik
http://www.bigaction.org/
 
Old April 7th, 2004, 03:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cool, cheers, Nik.

I'm using:

    //To generate our value: find out the biggest current value...
    $sql = "SELECT MAX(value) FROM table";
    $result = $db->getOne($sql);

    //Determine the numeric portion of that result...
    $numeric_portion = substr($result, 2);

    <new value> = "EV" . sprintf('%06u', ++$numeric_portion);

To increment the value.

(...and Tonight I'm to party like it's EV999999!)
 
Old April 7th, 2004, 04:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That looks fine to me. It's not necessary, but you might want to explicitly cast the "numeric portion" of the string as an integer.

$numeric_portion = (int)substr($result, 2);


Take care,

Nik
http://www.bigaction.org/
 
Old April 8th, 2004, 03:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very true. Unfortunately, I've inherited a rather large and untidy pile of code, and so my sense for the aesthetics of code is perhaps wearing a little thin ;).





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL script for selecting min and max values Nancy Krause Need help with your homework? 0 April 12th, 2007 11:29 AM
Adding Numeric TextBox Values virajoza HTML Code Clinic 1 December 18th, 2006 07:59 AM
How to set of Min/Max values of Line chart . Sanjay.Verma BOOK: Professional Crystal Reports for VS.NET 0 November 20th, 2006 11:35 PM
Calculating "Sum" of Numeric values in Varchar fld itHighway Classic ASP Basics 6 December 29th, 2004 07:41 PM
Choosing max values from different tables pontitt2 SQL Language 3 April 28th, 2004 05:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.