I am using variables in select query to calculate a value and based on the value I have added where condition in query.
What my problem in it gives addtional one more row whatever field value is.
For example if the field value is 1 on all rows in that table then it gives 20 plus additional one more row 21.
Kindly help in solving this issue.
How to add condtion based on value calculated as I can't use SUM, it will aggregate and returns only one value.
Below is sample query I use.
SET @var = 0;
SELECT @var := @var + field FROM table1 WHERE @var <= 20
MySql version 5.5.45
Check for
SELECT t1.(fieldset)
FROM source_table t1
JOIN source_table t2 ON t1.ordering_field <= t2.ordering field
GROUP BY t1.(fieldset)
HAVING SUM(t2.amount_field) <= @needed_sum
ORDER BY SUM(t2.amount_field) DESC [LIMIT 1]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.