0

如何在 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
4

4 回答 4

2

如果bank_id不是唯一的并且您想首先选择最高金额的记录,您可以尝试这个:

SELECT *
  FROM transaction
 WHERE transaction_id = 2
 ORDER BY bank_id DESC
        , amount DESC
 LIMIT 1

看到这个 SQLFiddle

于 2013-07-12T04:33:35.743 回答
1
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id desc
LIMIT 1
于 2013-07-12T04:20:29.450 回答
1

这是 SQLFiddel 演示

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
于 2013-07-12T06:15:22.037 回答
-1

试试这个..

SELECT *
  FROM transaction
 WHERE transaction_id = 2
 ORDER BY transaction_id DESC
 LIMIT 1
于 2013-07-12T04:38:09.453 回答