I have a table for formula grade where the user can create their own formula using the operator, column, dan number, which I have provided on app UI
formulaid | tipe | formula | gradecatid |
---|---|---|---|
1 | operator | ( | NULL |
1 | column | PAS | 5 |
1 | operator | + | NULL |
1 | column | PTS | 4 |
1 | operator | ) | NULL |
1 | operator | / | NULL |
1 | number | 2 | NULL |
the math string is from formula column, column type is another value from another grade type
( PAS + PTS ) / 2
last process will become string
( Number + Number ) / 2
Note:
This formula only the sample, the formula can be in any form for other example ( 5+1+2+3) / 4
, or 1 + 1
, it’s not fix to this form ( Number + Number ) / 2
, the user can create any formula with the given functionality as mention above, operator, number, and column.
Is it possible to evaluate this string in mysql to determine is valid math operation or not?
2
Answers
Finally I create this SP based on @Serg suggestion I try to create below SP for handling the error
it will show error if there's any bad math operation
Thanks All
You use this code via regex [mysql]
\+
\/
\*
\-
\(
\)
([0-9]+)
([0-9]+|[.|][0-9]+)