我有一列美元和欧元货币,并希望将价值乘以智利比索的相应交易所。到目前为止,我知道如何提取美元和欧元合约,但我不知道如何将每个价值乘以自己的汇率。我想它可能是这样的:
SELECT contracts.value, contracts.currency
FROM contracts
WHERE contracts.currency='USD' then contracts.value=contracts.value*500
OR contracts.currency='EUR' then contracts.value=contracts.value*750
感谢agian的帮助!
答案 0 :(得分:2)
您可以在select子句中执行此操作:
SELECT IF(contracts.currency='USD', contracts.value*500, contracts.value*750) as amount
FROM contracts
文档:http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
答案 1 :(得分:1)
您可以使用contracts.value
语句按contracts.currency
过滤CASE
:
SELECT contracts.currency
, CASE WHEN contracts.currency = 'USD' THEN contracts.value*500
WHEN contracts.currency = 'EUR' THEN contracts.value*750
ELSE contracts.value
END
FROM contracts
答案 2 :(得分:1)
如果您将拥有两种以上的货币,则可以使用查找表。
currency | factor
-----------------
USD | 500
EUR | 750
然后查询如下:
SELECT c.contract_id, c.contract, c.value, c.currency,
cc.factor, c.value * cc.factor AS calculatedValue
FROM contract c
INNER JOIN currency_conversion cc ON c.currency = cc.currency