limiting the rows to where the sum a column equals a certain value in MySQL

前端 未结 8 2466
闹比i
闹比i 2020-11-29 08:32

I want to write a query which returns all rows until the sum of one of the columns value reaches a certain value.

For example in the table below:

           


        
8条回答
  •  醉梦人生
    2020-11-29 09:15

    Here's a way to do it without a stored procedure:

    SET @msum := 0;
    SELECT t1.* 
    FROM (
        SELECT m.*,  
              (@msum := @msum + m.meetings) AS cumulative_meetings
        FROM meetings m 
        ORDER BY m.date ASC
    ) t1 
    WHERE t1.cumulative_meetings <= 7;
    

提交回复
热议问题