I would like to extract a value or its inverse from database. I would like to find a way to do the if statement in SQL, not in PHP.
My table looks like : id, rate, from_value, to_value
I have $from_value and $to_value variables, and would like to extract the corresponding rate value or its inverse.
I would like to
SELECT `rate` if `from_value` = $from_value and `to_value` = $to_value
or
SELECT (1/`rate`) if `from_value` = $to_value AND `to_value` = $from_value
with only one request.
I have searched a lot of ways to do that with CASE, IF, but I didn’t find a working solution.
I probably missed something. Thanks
Here is a portable method using
CASE. I have it returningNULLif neither condition is met.