如何获得最近3条记录

时间:2023-01-01 22:12:55

I am adding new record for every transaction

我为每笔交易添加新记录

For Example: microfinance,

例如:小额信贷,

  1. Today customer paid 300, then new record is inserted
  2. 今天客户付了300,然后插入新记录

  3. tomorrow 500 again new record is inserted)
  4. 明天500再插入新纪录)

then how can i get last 3 records based on customer id.

那我怎样才能根据客户ID获得最后3条记录。

table

ID               Password            Amount
1                  a                   200
2                  s                   500
1                  a                   100
3                  b                   200
1                  a                   300
1                  a                   300

there is no primary key

没有主键

i want last inserted 3 records for id=1

我想最后为id = 1插入3条记录

2 个解决方案

#1


0  

I believe you need something like this:

我相信你需要这样的东西:

SELECT *
FROM Table
ORDER BY id DESC
LIMIT 0,3

#2


0  

SELECT Id
      ,Password
      ,Amount
      ,@curRow := @curRow + 1 AS row_number
FROM sample JOIN  (SELECT @curRow := 0) r 
where id=1 
order by Row_number
desc limit 3;

Working Fiddle

#1


0  

I believe you need something like this:

我相信你需要这样的东西:

SELECT *
FROM Table
ORDER BY id DESC
LIMIT 0,3

#2


0  

SELECT Id
      ,Password
      ,Amount
      ,@curRow := @curRow + 1 AS row_number
FROM sample JOIN  (SELECT @curRow := 0) r 
where id=1 
order by Row_number
desc limit 3;

Working Fiddle