如何在mysql中更正这个查询当我使用MAX时,它给了我第一个事务

如何在mysql中更正这个查询当我使用MAX时,它给了我第一个事务,mysql,Mysql,所以我需要得到最后一个余额:1850,而我需要得到第一个余额2000:/ 非常感谢你的帮助 编辑 我得到了正确的答案,但它花费了太多的时间37秒,因为该表包含约400万条记录: trnx id accountid date accbalance 1 222 2014-04-16 2000 2 222

所以我需要得到最后一个余额:1850,而我需要得到第一个余额2000:/

非常感谢你的帮助

编辑

我得到了正确的答案,但它花费了太多的时间37秒,因为该表包含约400万条记录:

trnx id        accountid             date              accbalance
  1               222              2014-04-16            2000
  2               222              2014-04-16            1900
  3               222              2014-04-16            1850
  4 ...

您可以使用获取最后一个事务的not exists方法更轻松地执行此逻辑:

SELECT a.accountid,a.accountname, IFNULL(accountbalance,0)
FROM transactionstable tt
JOIN (SELECT accountid ,MAX(transactiondate) transactiondate,MAX(transactionid) transactionid
        FROM transactionstable
        WHERE transactiondate<"2014-04-15 23:59:59" 
        GROUP BY accountid
        HAVING MAX(transactiondate) ) t
USING ( transactionid )
RIGHT JOIN allaccounts a  ON (tt.accountid=a.accountid)
ORDER BY a.accountname ;
编辑:

如果您可以只使用transactionid,请尝试以下操作:

SELECT a.accountid, a.accountname, IFNULL(accountbalance,0)
FROM allaccounts a LEFT JOIN
     transactionstable tt
     ON tt.accountid = a.accountid
WHERE NOT EXISTS (SELECT 1
                  FROM transactionstable tt2
                  WHERE tt2.accountid = tt2.accountid and
                        (tt2.transactiondate > tt.transactiondate or
                         tt2.transactiondate = tt.transactiondate and
                         tt2.trans_id > tt.trans_id
                        )
                 )
ORDER BY a.accountname;

另外,还要在transactiontableaccountid、transactionid上创建一个索引。

transactionid是否保证始终增加,以便以后的事务始终具有更大的值?如果是,,您只需在您的派生表t中使用该列而不是transactiondate。不,这就是我没有使用它的原因:/n那么您怎么能希望确定哪一笔是最后一笔交易?您是对的,但交易应该按日期排序,但从几个月以来,系统中发生了一个错误,因此许多交易没有添加到表中事务表,因此我们必须在表的末尾手动添加它们。。当时没有太多像现在这样的在线用户,因此没有任何账户同时进行交易。谢谢你的评论,你这样想是对的:我没有看到你的答案。。在你删除它之前:/无论如何,谢谢你:这是目前最好的方法,因为从评论OP中可以看出,trans_id并不总是更大,可能会有这样一种情况,trans_id 3在trans_id 2之前,所以OP需要trans_id 2的行,所以我猜这种方法不能保证每个accountid组的最后一行,因为eggyal的最后一条评论是明确的无法识别最后一个transaction@MKhalidJunaid . . . 我将该评论解释为,尽管事务ID可能不会总体增加,但目标是在最新日期获得最大的事务ID。这可能不是整个事务id最大的一个。我试过了,。。我等了大约4分钟,结果没有得到结果,因为我的桌子很大。所以我不得不停止查询,以免影响在线用户。但谢谢你的回答:1027173毫秒。。。直到现在,所以我必须停止!!非常慢无论如何,非常感谢你试图帮助我:@twiry。我在编辑版本的子查询中有一个输入错误。现在可能行了。
SELECT a.accountid, a.accountname, IFNULL(accountbalance,0)
FROM allaccounts a LEFT JOIN
     transactionstable tt
     ON tt.accountid = a.accountid
WHERE NOT EXISTS (SELECT 1
                  FROM transactionstable tt2
                  WHERE tt2.accountid = tt2.accountid and
                        (tt2.transactiondate > tt.transactiondate or
                         tt2.transactiondate = tt.transactiondate and
                         tt2.trans_id > tt.trans_id
                        )
                 )
ORDER BY a.accountname;
SELECT a.accountid, a.accountname, IFNULL(accountbalance,0)
FROM allaccounts a LEFT JOIN
     transactionstable tt
     ON tt.accountid = a.accountid
WHERE NOT EXISTS (SELECT 1
                  FROM transactionstable tt2
                  WHERE tt2.accountid = tt.accountid and
                        tt2.transactionid > tt.transactionid
                 )
ORDER BY a.accountname;