我正在尝试编写带有某些 if 条件的 mysql 查询。
我的数据库结构是
货币(表)
++++++++++++++++++++++++++
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
++++++++++++++++++++++++++
我正在寻找的查询是 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);
}
我试图用 mysql 查询来实现这一点:我尝试了类似的方法,但它不起作用:
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
最佳答案
这可以简单地通过连接来完成,您不需要使用CASE EXPRESSION
:
SELECT t.*,
t.amt_paid / c.rate
FROM com_payments t
INNER JOIN currency c
ON(c.currency = p.amt_currency)
编辑:
SELECT t.*,
CASE WHEN c.currency = 'USD' THEN t.amt_paid * c.rate --By the way, you can use only amt_paid since rate is always 1
ELSE t.amt_paid / c.rate
END
FROM com_payments t
INNER JOIN currency c
ON(c.currency = p.amt_currency)
关于带有某些 IF ELSE If ELSE 条件的 Mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206127/