MySQL - How to select rows until condition is met I have a table that has a numeric field (call it NUM), I want to select rows until the sum of all NUM selected reaches some threshold. Is there an efficient way to do this?
Assuming:

CREATE TABLE T
(  x int not null primary key 
,  y int not null );

INSERT T(x,y)
VALUES (1, 1)
  ,(2, 1)
  ,(3, 2)
  ,(4, 8)
  ,(5, 3)
  ,(6, 5)
  ,(7, 4);


You can determine the cumulative sum like below, and then filter the result in an outer select:

select x,y,acc 
from (
    select x, y, (select sum(y) 
                  from T T2 
                  where T2.x &lt;= T1.x) as acc 
    from T T1
) as tmp 
where acc &lt; 10;

+---+---+------+
| x | y | acc  |
+---+---+------+
| 1 | 1 |    1 |
| 2 | 1 |    2 |
| 3 | 2 |    4 |
+---+---+------+


For DBMS that support window functions like sum(..) over ( ... ) much more elegant solutions exists.