PHP:应用不同表中的汇率

标签 php mysql

我正在研究一个金融系统的成本面板。我们有 4 种货币的费用:

PLN(波兰兹罗提)、美元、欧元和英镑。 表fin_costs存储所有成本记录。

enter image description here
还有一个名为 fin_exchange_rates 的表,用于存储这些货币每月的汇率。这些值由用户手动输入。

enter image description here

必须将汇率乘以amount_in_currency列中的值,并将结果值放入gross_amountnet_amount列中(相同的值)。表 fin_costs

中的这些列

这是我迄今为止所掌握的内容(截至 2013 年 2 月):

SELECT currency, amount_in_currency, EUR, USD, GBP,
CASE 
WHEN currency = 'EUR' THEN amount_in_currency * EUR
WHEN currency = 'USD' THEN amount_in_currency * USD
WHEN currency = 'GBP' THEN amount_in_currency * GBP
END AS new_value FROM fin_costs, fin_exchange_rates WHERE year_for_exchange = "2013" AND month_for_exchange = "2" AND year_analysis = "2013" AND month = "2"

其中new_value是应用汇率的金额。

问:如何更新每一行的新值?

任何帮助都将非常感激,因为我陷入了这个问题。

谢谢

最佳答案

这是一个适合您当前表结构的解决方案。不过,我强烈建议您考虑在 fin_exchange_rates 表中为每种货币换算设置一行。这样您就可以简单地加入适当的行,而不是使用所有 CASE 语句。

此外,此解决方案假设 fin_exhange_rates 表中的汇率与 PLN 货币相关。

UPDATE fin_costs fc
    JOIN fin_exchange_rates fer ON fc.year_analysis = fer.year_for_exchange
        AND fc.month = fer.month_for_exchange
SET fc.gross_amount = (CASE 
    WHEN fc.currency = 'EUR' THEN fc.amount_in_currency * fer.EUR
    WHEN fc.currency = 'USD' THEN fc.amount_in_currency * fer.USD
    WHEN fc.currency = 'GBP' THEN fc.amount_in_currency * fer.GBP
    WHEN fc.currency = 'PLN' THEN fc.amount_in_currency
    END),
fc.net_amount = (CASE 
    WHEN fc.currency = 'EUR' THEN fc.amount_in_currency * fer.EUR
    WHEN fc.currency = 'USD' THEN fc.amount_in_currency * fer.USD
    WHEN fc.currency = 'GBP' THEN fc.amount_in_currency * fer.GBP
    WHEN fc.currency = 'PLN' THEN fc.amount_in_currency
    END)

可以选择添加 WHERE 子句以限制为特定月份和/或年份。例如WHERE fc.month = 2 AND fc.year_analysis = 2013

SQLFiddle使用稍微修改过的数据

为了完整起见,以下是我将用来替换您当前的 fin_exchange_rates 表的表格...

CREATE TABLE `fin_currencies` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `abbreviation` varchar(16) NOT NULL,
  `name` varchar(64) NOT NULL,
  `displayCode` varchar(32) NOT NULL,
  PRIMARY KEY (`id`);

CREATE TABLE `fin_exchange_rates` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dateOfRate` date NOT NULL,
  `fromCurrencyID` int(11) NOT NULL,
  `toCurrencyID` int(11) NOT NULL,
  `rate` decimal(14,8) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `date_from_to` (`dateOfRate`,`fromCurrencyID`,`toCurrencyID`);

解决方案将变成这样......

UPDATE fin_costs fc
    JOIN fin_exchange_rates fer ON fc.year_analysis = YEAR(fer.dateOfRate)
        AND fc.month = MONTH(fer.dateOfRate)
    JOIN fin_currencies fcur1 ON fer.toCurrencyID = fcur1.id
      AND fcur1.abbreviation = fc.currency
    JOIN fin_currencies fcur2 ON fer.fromCurrencyID = fcur2.id
      AND fcur2.abbreviation = 'PLN'
SET fc.gross_amount = fc.amount_in_currency * fer.rate,
    fc.net_amount = fc.amount_in_currency * fer.rate

SQLFiddle以获得首选解决方案。

关于PHP:应用不同表中的汇率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23222641/

相关文章:

mysql - 将字符串视为 MySQL 语句?

php - htaccess 重写子域,没有文件名映射和扩展名

php - 如何填充表单以编辑现有数据

php - 子目录的 URL 无法正确重写

php - 如何获取未添加到数据库表中的完整月份日期?

PHP 选择同一行 3 次?

php - 如何制作一个php文章计数器?

php正则表达式拆分发票行项目描述

c# - 比较两列并在相同时增加计数

php - 我的 php session 文件夹中不需要的 session 文件