我想根据日期显示每位客户的最后2条记录,
例如:
id | name | date
1 | a | 2015-10-11
2 | a | 2015-09-11
3 | b | 2015-10-10
4 | b | 2015-09-01
我试过
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 WHERE cnt< = 2
但它显示所有记录
答案 0 :(得分:2)
SELECT * FROM( SELECT * FROM表ORDER BY id DESC LIMIT 2 )sub ORDER BY id ASC
这将从表格中选择最后2行,然后按升序排序。