In C++, you can do this:
T.x = (T.y > 1 && (T.x - T.y < 0)) ? 0 : (T.x - T.y)
which in [almost] plain english, is
if T.y > 1 and T.x-T.y < 0 then
set T.x to 0
else
set T.x to T.x-T.y
Is it possible to do the same thing using just SQL, without using stored procs or triggers?
Use the CASE
statement:
CASE WHEN T.y > 1 AND (T.x - T.y) < 0 THEN 0 ELSE (T.x - T.y) END
Yes its possible, take a look at the documentation, it says:
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.
This untested code should be your case:
SELECT IF(((T.y > 1) and (T.x-T.y < 0)), 0, (T.x-T.y))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With