Am not sure if this is possible. Let's say I have an arbitrary figure like 200, I have a table with two columns (both ints). Lets call the columns col_a
and col_b
.
I need to perform one query such that if col_a * col_b
for the first row does not equal to my arbitrary figure (in this case 200
), keep whatever value the first row equates to and go to second row, do the same while adding the results to the value we got from the first row, repeat this till the total equates to our arbitrary value.
In an example. Lets use our arbitrary figure of 200 say we have row 1 (col_a*colb) = 20
, row 2 (col_a*col_b) = 70
, row 3 (col_a*colb) = 110
, row 4 (col_a*col_b) = 30
.
My query should return row 1, row 2 and row 3. How is this possible in sql? I am using mysql.
Best Answer
You could use user defined variable in mysql to achieve your goal:
You must use
order by
to make sure of the rows order as suggested by ypercube. For example you can sum column in reverse order.