Question: I would like to select a value from a database table or its inverse with only one SQL Request.

I previoulsy posted a question, but now I have more specifications, which change the problem.

My table fields are : `id, rate, from_value, to_value, date_modified`

This is the pseudo-code I would like to do with only one request :

```
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 

WHERE UNIX_TIMESTAMP('NOW()')-".$expirationTime." < UNIX_TIMESTAMP('`date_modified`'))

```

I'm sure one of the row is present in the table, so I would like to return only the `rate` value, and null only if the WHERE clause is not realized. I don't want it to return NULL for the other rows (for which no conditions are realized). Thanks

Answer: 
Looks like a simple [CASE](http://dev.mysql.com/doc/refman/5.5/en/case-statement.html) will do the trick:

```
select
    case
        when from_value = $from_value and to_value = $to_value   then rate
        when from_value = $to_value   and to_value = $from_value then 1 / rate
    end
...

```

You might want an ELSE on the CASE if your WHERE clause doesn't force one of the CASE's branches to match.

If you only want rows that match one of the branches in the above CASE then use the WHERE clause:

```
select
    case
        when from_value = $from_value and to_value = $to_value   then rate
        when from_value = $to_value   and to_value = $from_value then 1 / rate
    end
where (from_value = $from_value and to_value = $to_value)
   or (from_value = $to_value   and to_value = $from_value)

```
haven't tried this out, but you can use an IF construct as follows

```
SELECT if from_value = $from_value and to_value = $to_value then rate else 1/rate end as   rate from tablename

```