Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2004
    Posts
    6

    Unanswered: Using Next and Previous in MySQL

    Hi:
    I am pretty new to MySQL. We are using MySQL 4.1.5 to write queries and incorporate them into Crystal Reports.

    Is there a 'Next' or 'Previous' function in MySQL? I have not found anything. I have a table like the following:

    Date1 | Date2 | Value
    =================================
    02/01/06 | 01/15/06 | 35
    02/01/06 | 01/20/06 | 38
    02/01/06 | 03/20/06 | 41
    ...

    What I need is (using the second line as an example):
    If (Date2 < Date1) Then MyAnswer = Previous(Value) = 35.

    How do I write a query statement to get the previous value?

    Can this be done using stored procedures?

    Thanks in advance.
    - SB

  2. #2
    Join Date
    Jan 2004
    Location
    Ft Collins, CO
    Posts
    22
    If you're just trying to return that one value, you could try using the LIMIT clause with an offset:

    Code:
    SELECT Value FROM Table WHERE Date2 < Date1 ORDER BY Date2 DESC LIMIT 1,1

  3. #3
    Join Date
    Dec 2004
    Posts
    6
    Thanks for that answer. It works for the single value, however, I will need to return more than one value. So, any other ideas?

    Thanks,
    - SB

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    how do you define "previous"? the rows in a database table have no physical order
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •