我有一张桌子在MySQL
结核病货币

currencyid | currency_name
CU0001     | IDR
CU0002     | SGD
CU0003     | USD

货币兑换器
currencyconverterid | currency_month | from_currencyid_fk | to_currencyid_fk | amount
CC0001              | 2018-03-01     | CU0001             | CU0002           | 0.00009
CC0002              | 2018-03-01     | CU0002             | CU0001           | 10425
CC0003              | 2018-03-01     | CU0003             | CU0002           | 1.31964

结核病预算
budgetid       | budget_month | departmentid_fk | currencyid_fk
BU201803000001 | 2018-03-01   | DP0003          | CU0002
BU201803000002 | 2018-03-01   | DP0002          | CU0002

结核病项目
itemid | item_name | currencyid_fk | price
IT0001 | Mouse     | CU0001        | 165000
IT0002 | Keyboard  | CU0002        | 20
IT0003 | TV LCD    | CU0003        | 350

tb_pro_请求
requestid      | budgetid_fk    | itemid_fk | quantity
RQ201803000001 | BU201803000001 | IT0002    | 1
RQ201803000002 | BU201803000001 | IT0003    | 5
RQ201803000003 | BU201803000001 | IT0004    | 1

例子:
我的departmentid_fk是:DP0003,这意味着我有货币SGD的预算。
在tb_pro_请求中,有两个项目使用不同的货币(IDR和美元)与预算部门货币(SGD)进行交易。
我想要的是,两个不同货币的项目需要先转换成SGD(因为我的部门预算是:新加坡元),然后再转换成SUM
*逻辑上,如果货币是SGD则不需要转换,但如果不是SGD则首先使用tb_currency_converters转换,然后使用SUM转换。
可以直接做查询吗?
到目前为止我的查询代码:
SELECT
    R.requestid,
    R.budgetid_fk,
    R.category,
    R.itemid_fk,
    SUM(R.quantity),
    R.request_date,
    R.investmentid_fk,
    R.remarks,
    R.approval_status,
    I.itemid,
    I.item_name,
    I.price,
    SUM(R.quantity) * I.price as total,
    B.budgetid,
    B.budget_month
FROM
    tb_pro_request R,
    tb_items I,
    tb_budgets B
WHERE
    R.itemid_fk = I.itemid AND
    R.budgetid_fk = B.budgetid AND
    R.investmentid_fk = '' AND
    B.active = 'Y' AND
    (R.approval_status = 'P' OR R.approval_status = 'A') AND
    DATE_FORMAT(B.budget_month,'%Y-%m') = '2018-03' AND
    B.departmentid_fk = 'DP0003'
    GROUP BY I.currencyid_fk

您可以看到下面有3个不同货币的项目。
我想要的,兑换成新加坡元兑换成印尼盾,美元。然后求和(1行)
mysql - 基于另一个表的MySQL SELECT SUM-LMLPHP

最佳答案

我想我把你的FK搞砸了,但至少你有精神;)
SQL Fiddle
MySQL 5.6模式设置:
问题1:

SELECT
    R.budgetid_fk,
    SUM(R.quantity),
    SUM(R.quantity * I.price * COALESCE(CC.amount,1)) as total,
    B.budgetid,
    B.budget_month
FROM tb_pro_request R
INNER JOIN tb_items I
  ON R.itemid_fk = I.itemid
INNER JOIN tb_budgets B
  ON R.budgetid_fk = B.budgetid
  AND B.active = 'Y'
LEFT JOIN tb_currency_converters CC
  ON CC.from_currencyid_fk = I.currencyid_fk
  AND CC.to_currencyid_fk = B.currencyid_fk
WHERE
    R.investmentid_fk = ''
    AND (
      R.approval_status = 'P'
      OR R.approval_status = 'A'
    )
    AND DATE_FORMAT(B.budget_month,'%Y-%m') = '2018-03'
    AND B.departmentid_fk = 'DP0003'
GROUP BY R.budgetid_fk

Results
|    budgetid_fk | SUM(R.quantity) |             total |       budgetid | budget_month |
|----------------|-----------------|-------------------|----------------|--------------|
| BU201803000001 |               7 | 575.2840143424692 | BU201803000001 |   2018-03-01 |

关于mysql - 基于另一个表的MySQL SELECT SUM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49104596/

10-10 16:21