0

假设我有

SELECT `Type`, `Originating Country Code`,`Originating Currency Code`, SUM(`Principal Amount`) as Total 
FROM `colab` 
WHERE (`Destination Country Code`='PH' 
    AND `Destination Currency Code`='PHP'
    AND `Type` = 'PAID') || (`Destination Country Code`='PH' 
    AND `Destination Currency Code`='USD'
    AND `Type`= 'PAID')
    GROUP BY `Type`

工作正常并返回true,请参见下文

country_code | Currency_Code  |   Type    |  Principal

=======================================================
| PH         |   PHP          |    PAID   |    200
=======================================================

我的问题是我想将货币的总本金与美元值分开。请查看我的数据库中的记录

country_code | Currency_Code  |   Type    |  Principal

=======================================================
| PH         |   PHP          |    PAID   |    100
| PH         |   USD          |    PAID   |    100
=======================================================
4

5 回答 5

3

试试这个查询:

SELECT `Currency_Code`, SUM(`Principal Amount`) as Total 
FROM `table`
GROUP BY `Currency_Code`
于 2013-03-10T03:17:06.500 回答
1

你可以这样使用GROUP BY

SELECT `Currency_Code`, SUM(`Principal Amount`) as Total 
FROM `table` 
WHERE `Country Code`='PH'
    AND `Type` LIKE '%W%'
GROUP BY `Currency_Code`

如果要添加更多条件,只需使用or在WHERE子句中添加更多部分,例如:ANDOR

SELECT `Currency_Code`, SUM(`Principal Amount`) as Total 
FROM `table` 
WHERE `Country Code`='PH'
    AND `Type` LIKE '%W%'
    AND `Currency_Code` IN('USD', 'CAD', 'YEN')
    AND `Principal_Amount` > 10
GROUP BY `Currency_Code`
于 2013-03-10T03:18:40.253 回答
0

你需要使用GROUP BY

IE

SELECT `Currency Code`, SUM(`Principal Amount`) as Total 
FROM `table` 
WHERE `Country Code`='PH' 
    AND `Type` LIKE '%W%'
GROUP BY `Currency Code`
于 2013-03-10T03:17:53.720 回答
0

您不能根据您的要求更改查询吗?我的意思是根据您的预期结果,我推断您需要基于 Type 或 country_code 的结果,因此Currency Code从查询中删除“AND ='PHP'”将为您提供预期的结果。

于 2013-03-10T03:24:30.020 回答
0

将您的条件分解为子查询并加入它。只需添加任意数量的规则即可。

SELECT `Country Code`, `Currency Code`, `Type`,
       SUM(`Principal Amount`) as Total 
FROM `table` 
JOIN (
    SELECT 'PH' as a, 'PHP' as b, '%W%' as c
    UNION SELECT 'PH', 'USD', '%W%'
    UNION SELECT 'PY', 'PYC', '%H%'
) AS criteria
    ON (`table`.`Country Code` = criteria.a and
        `table`.`Currency Code` = criteria.b and
        `table`.`Type` like criteria.c)
GROUP BY `Country Code`, `Currency Code`, `Type`
于 2013-03-10T03:31:13.450 回答