Jump to content

Next Number In Sequence Following Specific Value


dlebowski

Recommended Posts

Ok. I have spent a couple of days on this and have had no luck figuring it out. It seems easy enough, but apparently i'm not smart enough to figure it out.

 

Anyway, here is what I want to do:

 

Table "lots" has the following column:

 

LotNumber

1

2

4

5

6

7

100

102

104

 

Here is my code:

 

$j = mysql_num_rows($res);
$i = 0;
while ($i++ < $j and mysql_result($res, $i - 1, 0) == $i);

 

 

This code works great if I need the next available number starting from "0". That value would be "3". But lets say I want to get the next available number starting at 100. How do I do that? For example, the next available number after 100 is "101". I can't get it to do this for me. Any help would be appreciated. Thank you in advance.

 

Ryan

Link to comment
Share on other sites

Hmm. You can do a LEFT JOIN from the table back onto itself looking at the current lot number + 1, and if the other side is NULL then there's no matching row and you can return the number + 1.

SELECT one.LotNumber + 1 FROM lots one LEFT JOIN lots two ON one.LotNumber + 1 = two.LotNumber WHERE two.LotNumber IS NULL ORDER BY one.LotNumber ASC LIMIT 1

Give or take.

Link to comment
Share on other sites

Thank you for the reply! What if I want to specify whether I start with "0" or "100" as my starting point? AND I only wanted it for a certain date.

 

So for example:

If I start with "0" and date of 2012-11-15, the next available number is: 3.

If I start with "101" and date of 2012-11-15, the next available number is: 103.

 

Also, what if my table looked like this:

 

LotNumber Date

1 2012-11-15

2 2012-11-15

3 2011-10-12

4 2012-11-15

5 2012-11-15

6 2012-11-15

7 2012-11-15

100 2012-11-15

102 2012-11-15

103 2011-10-12

104 2012-11-15

Link to comment
Share on other sites

Table "lots" has the following column:

 

LotNumber

 

then

 

Also, what if my table looked like this:

 

LotNumber Date

 

If there is one thing developers love it's people telling us one thing then telling us that that wasn't the whole truth.

 

If you are serious about wanting help, tell the whole story - not bits at a time.

Link to comment
Share on other sites

I would just do a query and loop through it to get the next number. For example

 


$lotNumber = 100;

//this gets all lot numbers between 100 and 200
$qry = "SELECT lotNumber FROM lots WHERE lotNumber BETWEEN $lotNumber AND ".($lotNumber + 100)." ORDER BY lotNumber";

$result = mysql_query($qry);

while($row = mysql_fetch_array($result))
{
if($row[0] != $lotNumber)
break;
else
$lotNumber++;
}
[/Code]

 

Now the next available number is $lotNumber and just insert the value in to your db

Edited by akphidelt2007
Link to comment
Share on other sites

Thank you for the reply! What if I want to specify whether I start with "0" or "100" as my starting point? AND I only wanted it for a certain date...

What I said before still works, just constrain it by the lower LotNumber limit (on the first table) and the date on the second table being before the date you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.