我正在使用两个数据库。一个数据库在Microsoft SQL 2012上,另一个在MySQL 5.5上。 Microsoft SQL Server有一个Orders表,MySQL有三个货币汇率表。订单表有来自6个不同国家的订单;但是,来自3个国家的订单的销售总额为当地货币(非美国)。 3种货币汇率表按国家/地区分隔,包含每日汇率。使用PHP,我需要按月和国家分组当前年度总销售额,并将汇率应用于那些不是美国货币的国家总销售额。每个月的汇率必须是该月的平均汇率。
下面有两个数组。第一个数组用于销售总额并按月和国家分组的订单。第二个数组是货币汇率,其平均汇率按月和国家计算和分组。
我如何将每个月的总销售额乘以非美元国家(KR,JP和NZ)的汇率?
$query = $db->prepare("SELECT MONTH(US_OrderDate) AS MONTH, a.Country, SUM(a.Sales) AS Total_Sales FROM (
SELECT
CASE
WHEN Country = 'KR' THEN DATEADD(DAY, +1, CAST(OrderDate AS Date))
ELSE CAST(OrderDate as Date) END AS US_OrderDate, Country, SUM(Total) AS Sales FROM Orders
WHERE (OrderStatusID in ('7','8','9') AND Country = 'KR')
OR (OrderStatusID in ('7','8','9') AND Country = 'JP')
OR (OrderStatusID in ('7','8','9') AND Country = 'AU')
OR (OrderStatusID in ('7','8','9') AND Country = 'NZ')
OR (OrderStatusID in ('7','8','9') AND Country = 'US')
OR (OrderStatusID in ('7','8','9') AND Country = 'CA')
GROUP BY CAST(OrderDate as Date),Country) AS a
WHERE YEAR(US_OrderDate) = YEAR(GETDATE())
GROUP BY MONTH(US_OrderDate), a.Country
Order BY MONTH(US_OrderDate) ASC
");
try {
$query->execute();
$results = $query->fetchALL(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
die("{error: {$e->getMessage()}}");
}
array(54) {
[0]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "AU"
["Total_Sales"]=>
string(7) "9095.70"
}
[1]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "CA"
["Total_Sales"]=>
string(9) "113993.00"
}
[2]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "KR"
["Total_Sales"]=>
string(7) "9284.75"
}
[3]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
string(6) "883.25"
}
[4]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "609538.25"
}
[5]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "AU"
["Total_Sales"]=>
string(7) "7393.65"
}
[6]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "CA"
["Total_Sales"]=>
string(9) "100279.43"
}
[7]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "JP"
["Total_Sales"]=>
string(9) "916110.00"
}
[8]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "KR"
["Total_Sales"]=>
string(7) "5217.55"
}
[9]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "NZ"
["Total_Sales"]=>
string(7) "1355.85"
}
[10]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "US"
["Total_Sales"]=>
string(9) "616422.11"
}
....
$query_exchange_rate = $db_exchange_rate->prepare("SELECT Month(date) AS MONTH, 'NZ' AS Country, ROUND((SUM(rate)/COUNT(rate)),5) AS AVG_RATE FROM currency_exchange_rates.new_zealand WHERE YEAR(date) = YEAR(CURDATE()) GROUP BY MONTH(date)
UNION
SELECT Month(date) AS MONTH, 'JP' AS Country, ROUND((SUM(rate)/COUNT(rate)),5) AS AVG_RATE FROM currency_exchange_rates.japan WHERE YEAR(date) = YEAR(CURDATE()) GROUP BY MONTH(date)
UNION
SELECT Month(date) AS MONTH, 'KR' AS Country, ROUND((SUM(rate)/COUNT(rate)),5) AS AVG_RATE FROM currency_exchange_rates.south_korea WHERE YEAR(date) = YEAR(CURDATE()) GROUP BY MONTH(date)
ORDER BY MONTH ASC");
try {
$query_exchange_rate->execute();
$results_exchange_rate = $query_exchange_rate->fetchALL(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
die("{error: {$e->getMessage()}}");
}
array(27) {
[0]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "NZ"
["AVG_RATE"]=>
string(7) "0.76614"
}
[1]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "JP"
["AVG_RATE"]=>
string(7) "0.00845"
}
[2]=>
array(3) {
["MONTH"]=>
string(1) "1"
["Country"]=>
string(2) "KR"
["AVG_RATE"]=>
string(7) "0.00090"
}
[3]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "NZ"
["AVG_RATE"]=>
string(7) "0.74309"
}
[4]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "JP"
["AVG_RATE"]=>
string(7) "0.00842"
}
[5]=>
array(3) {
["MONTH"]=>
string(1) "2"
["Country"]=>
string(2) "KR"
["AVG_RATE"]=>
string(7) "0.00090"
}
答案 0 :(得分:1)
只需循环浏览它们。一种简单的方法是循环遍历较长的一个,每次循环一个短的。
foreach($results as &$a) // Need to be by reference to update it easily
foreach($results_exchange_rate as $b)
if($a['MONTH'] == $b['MONTH'] && $a['Country']==$b['Country'])
$a['Total_Sales']*= $b['AVG_RATE'];
每次月份和国家/地区在两个阵列中相同时,这将使总销售额乘以平均费率。