troubleshooting Question

Fields of last 2 records

Avatar of myyis
myyis asked on
MySQL Server
5 Comments1 Solution450 ViewsLast Modified:
Using the solution here http://stackoverflow.com/questions/10314953/diff-value-last-two-record-by-datetime I am trying to find the values of the last 2 records,
but it turns out that it gives me the last and the first record.

Can anybody correct that for me?
Thank you


SELECT ORID, value1,value2
FROM (  SELECT h.ORID, h.DATETIME AS value1, h2.DATETIME AS value2
        FROM (  SELECT id, ORID, DATETIME
                FROM PAYMENT
                GROUP BY ORID
                ORDER BY ID DESC) AS h
        INNER JOIN (    SELECT id, ORID, DATETIME
                        FROM PAYMENT
                        ORDER BY ID DESC) AS h2
        ON h.ORID = h2.ORID AND h.id != h2.id AND h2.ORID=988
        GROUP BY ORID) AS h3
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros