如何在 MySQL 上选择此结果的最后一个值:
这是查询的结果:
SELECT * from transaction WHERE transaction_id = 2
我只需要最后一个值3 300 2
bank_id amount transaction_id
1 800 2
3 50 2
3 300 2
如果bank_id
不是唯一的并且您想首先选择最高金额的记录,您可以尝试这个:
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id DESC
, amount DESC
LIMIT 1
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id desc
LIMIT 1
Transaction
此演示使用您的过滤器选择表中的最后一个条目Transaction_ID = 2
。
以下是您可以尝试的查询。
select *,@curRow := @curRow + 1 AS row_number
from Temp
Join (SELECT @curRow := 0) r
where Transaction_id = 2
order by row_number desc
limit 1
试试这个..
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY transaction_id DESC
LIMIT 1