MySQL将表1中的多个列连接到表2中的多个列

时间:2013-11-07 06:46:31

标签: mysql sql sql-server

对,所以这是我的问题, 我有2个表,orderentry和standardexpenses orderentry(如O)看起来像

ORDERNO | FROMSTATIONCODE | TOSTATIONCODE

和标准索引(SE)看起来像

FROMSTATION | TOSTATION | TOTALAMOUNT | CURRENCY (which is either in MT or USD)

我已经想出如何使用

转换货币
ROUND(SUM(IF(SE.CURRENCY='MT',SE.TOTALAMOUNT*0.0336,SE.TOTALAMOUNT)),2)

我想知道的是我如何加入O.FROMSTATIONCODEO.TOSTATIONCODE加入SE.FROMSTATIONSE.TOSTATION ...但仍然可以计算{{ 1}}

因为现在我的代码是

SUM(TOTALAMOUNT).

注意:例如。 SE有5个不同的费用,从destination1到destionation2,ORDERENTRY有12个

SELECT FROMSTATION, TOSTATION, SUM(TOTALAMOUNT), COUNT(TOTALAMOUNT), CURRENCY FROM STANDARDEXPENSES JOIN ORDERENTRY AS O ON STANDARDEXPENSES.FROMSTATION = O.FROMSTATIONCODE AND STANDARDEXPENSES.TOSTATION = O.TOSTATIONCODE GROUP BY FROMSTATION,TOSTATION,CURRENCY 从destination1到destionation2,我想使用5个费用的总和
(转换后)并将其应用于所有12 ORDERNO,以便它们各自显示相同的TOTALAMOUNT。

现在我尝试它,它给了我似乎是随机倍数而不是实际数量。

1 个答案:

答案 0 :(得分:0)

我不确定这是否适用于MySQL,但您可以尝试这样的事情。如果问题是如何转换汇总TOTALAMOUNT中的货币值。

SELECT 
FROMSTATION,
TOSTATION,
CASE CURRENCY WHEN 'MT' THEN 0.0336 ELSE 1.00 END AS FX_RATE,
ROUND(SUM(TOTALAMOUNT*FX_RATE),2),
COUNT(TOTALAMOUNT),
CURRENCY
FROM STANDARDEXPENSES
JOIN ORDERENTRY AS O 
ON STANDARDEXPENSES.FROMSTATION = O.FROMSTATIONCODE
AND STANDARDEXPENSES.TOSTATION = O.TOSTATIONCODE
GROUP BY FROMSTATION,TOSTATION,CURRENCY