且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将行限制为一列总和等于 MySQL 中的某个值

更新时间:2023-02-10 23:29:37

这是一种应该在 MySQL 中工作的方法:

Here's a way which should work in MySQL :

SELECT
  O.Id,
  O.Type,
  O.MyAmountCol,
  (SELECT
     sum(MyAmountCol) FROM Table1
   WHERE Id <= O.Id) 'RunningTotal'
FROM Table1 O
HAVING RunningTotal <= 7

它涉及计算运行总数并选择运行总数小于或等于给定数字的记录,在本例中为7.

It involves calculating a running total and selecting records while the running total is less than or equal to the given number, in this case 7.