gpt4 book ai didi

带有某些 IF ELSE If ELSE 条件的 Mysql 查询

转载 作者:行者123 更新时间:2023-11-29 20:27:42 25 4
gpt4 key购买 nike

我正在尝试编写带有某些 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com