If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Using Next and Previous in MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-06, 14:21
sbrenner sbrenner is offline
Registered User
 
Join Date: Dec 2004
Posts: 6
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
Reply With Quote
  #2 (permalink)  
Old 02-16-06, 14:42
billdo75 billdo75 is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 02-16-06, 15:12
sbrenner sbrenner is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 02-17-06, 19:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
how do you define "previous"? the rows in a database table have no physical order
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On