MYSQL中的UNION和合并列

时间:2014-08-14 15:17:45

标签: php mysql sql

我从mysql中的两个表中获取数据。我有这样的查询:

SELECT SUM(IF(debit IS NULL, 0, debit)) - SUM(IF(credit IS NULL, 0, credit)) as amount, th.country_id, cl.currency_name
FROM account_treasury_hq th
LEFT JOIN system_country_list cl ON cl.country_id=th.country_id
WHERE th.company_id='$company_id'
GROUP BY th.country_id

UNION

SELECT SUM(CASE WHEN ce_type = 'IN' or ce_type is NULL then payment_amount
                WHEN ce_type = 'OUT' then - payment_amount
                END) as amount,
source_country_id as country_id, cl.currency_name
FROM customer_payment_options cpo
LEFT JOIN system_country_list cl ON cl.country_id=cpo.source_country_id
WHERE cpo.company_id='$company_id'
GROUP BY cpo.source_country_id

结果

Array
(
[0] => Array
    (
        [amount] => -345
        [country_id] => 40
        [currency_name] => Canadian dollar
    )

[1] => Array
    (
        [amount] => 93
        [country_id] => 210
        [currency_name] => Sri Lankan rupee
    )

[2] => Array
    (
        [amount] => 46.5
        [country_id] => 236
        [currency_name] => United States dollar
    )

[3] => Array
    (
        [amount] => 916249.8999999999
        [country_id] => 40
        [currency_name] => Canadian dollar
    )

[4] => Array
    (
        [amount] => -6670.1
        [country_id] => 210
        [currency_name] => Sri Lankan rupee
    )

[5] => Array
    (
        [amount] => 2017.67
        [country_id] => 236
        [currency_name] => United States dollar
    )

)

在结果数组中,您可以看到相同的country_id。我想将这些相同的国家/地区ID与1合并。这意味着,每个country_id应该只有每个金额信息。 (amount应添加多个国家/地区) 有没有办法在PHPmysql本身执行此操作? 感谢您提供任何帮助。

必填结果

Array
(
[0] => Array
    (
        [amount] => 915904.89
        [country_id] => 40
        [currency_name] => Canadian dollar
    )

[1] => Array
    (
        [amount] => -6577.1
        [country_id] => 210
        [currency_name] => Sri Lankan rupee
    )

[2] => Array
    (
        [amount] => 2064.17
        [country_id] => 236
        [currency_name] => United States dollar
    )
)

3 个答案:

答案 0 :(得分:1)

只需在外部查询中选择全部,并按country_id

分组金额和组
SELECT 
    SUM(amount) country_id, currency_name
FROM 
(   SELECT 
        SUM(IF(debit IS NULL, 0, debit)) - SUM(IF(credit IS NULL, 0, credit)) as amount,
        th.country_id, 
        cl.currency_name
    FROM account_treasury_hq th
    LEFT JOIN system_country_list cl ON cl.country_id=th.country_id
    WHERE th.company_id='$company_id'
    GROUP BY th.country_id

    UNION

    SELECT 
        SUM(CASE 
               WHEN ce_type = 'IN' or ce_type is NULL then payment_amount
               WHEN ce_type = 'OUT' then - payment_amount
               END
        ) as amount,
        source_country_id as country_id, 
        cl.currency_name
    FROM customer_payment_options cpo
    LEFT JOIN system_country_list cl ON cl.country_id=cpo.source_country_id
    WHERE cpo.company_id='$company_id'
    GROUP BY cpo.source_country_id
) t
GROUP BY country_id

答案 1 :(得分:1)

您正在执行union,并在单个子查询中进行分组。联合中的每个子查询都不知道其他并行查询正在做什么。每个查询的group by仅适用于该一个查询。

您需要在包装器查询中执行繁重的工作,例如

SELECT SUM(.....)
FROM (
     SELECT th.country_id AS country_id, etc...
     UNION ALL
     SELECT source_country AS country_id, etc...
) AS foo
GROUP BY country_id

e.g。做所有的分组和"数学"在外部查询中,内部查询只是检索您想要组合在一起的所有记录。

答案 2 :(得分:-1)

没有测试过这个(需要一个sqlfiddle来玩)但是你想在查询中聚合它们。像这样的东西(你想要调整小组并根据你的需要选择):

SELECT SUM( x.amount ), x.country_id, x.currency_name
FROM
(
    SELECT SUM(IF(debit IS NULL, 0, debit)) - SUM(IF(credit IS NULL, 0, credit)) as     amount, th.country_id, cl.currency_name
    FROM account_treasury_hq th
    LEFT JOIN system_country_list cl ON cl.country_id=th.country_id
    WHERE th.company_id='$company_id'
    GROUP BY th.country_id

    UNION

   SELECT SUM(CASE WHEN ce_type = 'IN' or ce_type is NULL then payment_amount
            WHEN ce_type = 'OUT' then - payment_amount
            END) as amount,
source_country_id as country_id, cl.currency_name
   FROM customer_payment_options cpo
   LEFT JOIN system_country_list cl ON cl.country_id=cpo.source_country_id
   WHERE cpo.company_id='$company_id'
   GROUP BY cpo.source_country_id
) AS x
GROUP BY x.amount, x.country_id, x.currency_name