A big part of improving from bad to good programming is learning how to write the requirement so clearly, a 12-year-old novice understands it instantly.
First, it's a design error to define a column as text when arithmetic will be done on its contents. It needs to be int or decimal.
Then, your statement of the requirement ...
WHERE srvid=1 AND if(QID <> GivenQID, TRUE, if(TheQuery, TRUE, FALSE)) = TRUE
TheQuery: SUM(Answer) of any specific MX >= GivenValue
... entails that the query know sum(`answer`) before it runs. Obviously not possible.
The requirement appears to be at least a 2-step ...
1 Find rows for which sum(`answer`) >= givenvalue. For this, you'll need to specify exactly how to define which rows are to be thus Grouped By.
2 Then apply the other criteria referenced above to those rows.
Here's a little toy table we could use for working this up ...
drop table if exists t;
create table t (srvid int, qid int, mx int, ma int, answer int );
insert into t values
Edited 1 time(s). Last edit at 04/08/2022 12:15PM by Peter Brawley.