> One solution could be something like
>
>   SELECT id, first, last
>   FROM   names
>   WHERE last > '$previouslast' AND first > 'previousfirst'
>   ORDER BY last, first
>   LIMIT 1
>
> That is: Take all results that "come after" the previous name, sort
> them and then take only the first.
>
> The problem with it is that it only works fine as long as (last,first)
> is unique. If not, you will end up looping on the same (last,first)
> pair forever.


I don't think it would loop forever, since you search by '>', not '>=', so
any identical names would be skipped.  Either way, though, last,first, must
be unique.  A fix to this might be to order by id after this, since id is
unique

   SELECT id, first, last
   FROM   names
   WHERE last > 'previouslast' AND first > 'previousfirst' AND
id>'$previousid'
   ORDER BY last, first, id
   LIMIT 1

Thus sorting through any identical names as well.  (How many Bob Smiths can
there be?)

However, this brings out a problem in using AND to combine the elements.  by
saying  "WHERE last > '$previouslast' AND first > 'previousfirst'
", we're falsly stating that the first name _must_ increase along with the
last name, where in reality the first name may decrease, so long as the last
name increases.  Follow? Here:

Elliott, Nicholas
Smith, Bob

After Finding Elliott, Nicholas, the Query would _not_ find Bob Smith, even
though 'Smith'>'Elliott', because 'Bob' is not greater that 'Nicholas'.

So, whats the best way to rewrite this?  Perhaps a Concat?

SELECT id, first, last
   FROM   names
   WHERE CONCAT(last,first,id)>CONCAT('$previouslast', '$previousfirst',
'$previousid')
   ORDER BY last, first, id
   LIMIT 1

I think that would work, feel free to correct me!

Nicholas Elliott


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to