1

I want to display last 2 records for each customer based on date, eg:

      id   |   name   |   date
       1   |    a     |  2015-10-11
       2   |    a     |  2015-09-11
       3   |    b     |  2015-10-10
       4   |    b     |  2015-09-01

I tried like

SELECT id,cust_id FROM ( SELECT id,cust_id @currcount := IF(@currvalue = cust_id, @currcount + 1, 1) AS cnt FROM customer ORDER BY id DESC) AS whatever WHERE cnt <= 2

but it displays all records

Ganesh Gudghe
  • 1,327
  • 1
  • 17
  • 41

1 Answers1

0

SELECT * FROM ( SELECT * FROM table ORDER BY id DESC LIMIT 2 ) sub ORDER BY id ASC

This will select the last 2 rows from table, and then order them in ascending order.