I have this table, let's call it Lines:
id | id_sale | id_product | quantity
1      1           1           4
2      2           1           4
3      3           1           4
4      4           1           4
It represents the lines of a sale.
And I want to fetch the id of the line where the product gets sold X times. For something like this I believe that an order by id asc is needed.
For instance, with the previous data, for product 1 and to X = 6, I would get the id 2. Or for X = 10 I would get the id 3.
I have found a question that's pretty much what I need: use mysql SUM() in a WHERE clause
I have tried the correct answer (adapted for SQL) but in SQL I can't add the order by id inside the subquery, and because of this I believe the query stops making sense.
Here is what I've tried so far:
select y.id, y.quantity 
from (select t.id, 
            t.quantity, 
            (select sum(x.quantity) 
                from Lines x
                where x.id <= t.id) as atmTotal
        from Lines t where t.id_product = 1) y
where y.atmTotal >= 10
order by y.ID;
Result: This is just returning all id's (and quantities) that have the product 1.
 
     
     
    