lundi 29 août 2016

Mysql query with certain IF ELSE If ELSE Conditions

Iam trying to write a mysql query with certain if conditions.

My DB Structure is

Currency (Table)

++++++++++++++++++++++++++
id | currency | rate
++++++++++++++++++++++++++
1      USD       1
2      INR       67.10043
3      GBP       0.761203
4      EUR       0.89295
++++++++++++++++++++++++++

com_payments (Table)
++++++++++++++++++++++++++
id | amt_curr | amt_paid
++++++++++++++++++++++++++
1      EUR        300.89
2      GBP        390.90
3      USD        600.00
++++++++++++++++++++++++++

The query i am looking for is if

if (amt_curr=='USD') {
    $totalinr = (amt_paid * rate); (rate it should take from currency table where currency='USD' from currency table)
} else if ($currency1=='GBP'){
    totalinr = ((select currency, rate from Currency where currency=INR / 0.761203 (GBP value from currency table)) * amt_paid);
} else if ($currency1=='EUR'){
    totalinr = ((select currency, rate from Currency where currency=INR / 0.89295 (EUR value from currency table)) * amt_paid); 
}

Iam trying to achieve this with mysql query: I tried something like this but its not working:

CASE WHEN (amt_curr=='USD') THEN (amt_paid * rate) (rate it should take from currency table where currency='USD' from currency table)
     WHEN ($currency1=='GBP') THEN ((select currency, rate from Currency where currency=INR / 0.761203 (GBP value from currency table)) * amt_paid) 
     WHEN ($currency1=='EUR') THEN ((select currency, rate from Currency where currency=INR / 0.89295 (EUR value from currency table)) * amt_paid)
END AS totalinr

Aucun commentaire:

Enregistrer un commentaire