0

试图将两个查询的结果组合在一起。当我运行此查询时:

SELECT pr_id,
        pr_sbtcode,
        pr_sdesc,
        od_quantity,
        od_amount
FROM (
        SELECT `bgProducts`.`pr_id`,
                `bgProducts`.`pr_sbtcode`,
                `bgProducts`.`pr_sdesc`,
                SUM(`od_quantity`) AS `od_quantity`,
                SUM(`od_amount`) AS `od_amount`,
                MIN(UNIX_TIMESTAMP(`or_date`)) AS `or_date`
        FROM `bgOrderMain`
        INNER JOIN `bgOrderData`
        INNER JOIN `bgProducts`
        WHERE `bgOrderMain`.`or_id` = `bgOrderData`.`or_id`
                AND `od_pr` = `pr_id`
                AND UNIX_TIMESTAMP(`or_date`) >= '1262322000'
                AND UNIX_TIMESTAMP(`or_date`) <= '1346990399'
                AND (
                        `pr_id` = '415'
                        OR `pr_id` = '1088'
                        )
        GROUP BY `bgProducts`.`pr_id`

        UNION

        SELECT `bgProducts`.`pr_id`,
                `bgProducts`.`pr_sbtcode`,
                `bgProducts`.`pr_sdesc`,
                SUM(`od_quantity`) AS `od_quantity`,
                SUM(`od_amount`) AS `od_amount`,
                MIN(UNIX_TIMESTAMP(`or_date`)) AS `or_date`
        FROM `npOrderMain`
        INNER JOIN `npOrderData`
        INNER JOIN `bgProducts`
        WHERE `npOrderMain`.`or_id` = `npOrderData`.`or_id`
                AND `od_pr` = `pr_id`
                AND UNIX_TIMESTAMP(`or_date`) >= '1262322000'
                AND UNIX_TIMESTAMP(`or_date`) <= '1346990399'
                AND (
                        `pr_id` = '415'
                        OR `pr_id` = '1088'
                        )
        GROUP BY `bgProducts`.`pr_id`
        ) TEMPTABLE3;

它产生这个结果

+--------+--------------+----------------+---------------+------------+
| pr_id  |  pr_sbtcode  |  pr_sdesc      |  od_quantity  |  od_amount |
+--------+--------------+----------------+---------------+------------+
|   415  |  NP13        |  Product 13    |  5            |  125       |
|  1088  |  NPAW        |  Product AW    |  4            |  100       |
|   415  |  NP13        |  Product 13    |  5            |  125       |
|  1088  |  NPAW        |  Product AW    |  2            |  50        |
+--------+--------------+----------------+---------------+------------+ 

我想得到一个将它们组合成两行的结果:

+--------+--------------+----------------+---------------+------------+ 
| pr_id  |  pr_sbtcode  |  pr_sdesc      |  od_quantity  |  od_amount |
+--------+--------------+----------------+---------------+------------+ 
|   415  |  NP13        |  Product 13    |  10           |  250       |
|  1088  |  NPAW        |  Product AW    |  6            |  150       | 
+--------+--------------+----------------+---------------+------------+

所以我将 GROUP BY pr_id 添加到查询的末尾:

SELECT pr_id,
        pr_sbtcode,
        pr_sdesc,
        od_quantity,
        od_amount
FROM (
        SELECT `bgProducts`.`pr_id`,
                `bgProducts`.`pr_sbtcode`,
                `bgProducts`.`pr_sdesc`,
                SUM(`od_quantity`) AS `od_quantity`,
                SUM(`od_amount`) AS `od_amount`,
                MIN(UNIX_TIMESTAMP(`or_date`)) AS `or_date`
        FROM `bgOrderMain`
        INNER JOIN `bgOrderData`
        INNER JOIN `bgProducts`
        WHERE `bgOrderMain`.`or_id` = `bgOrderData`.`or_id`
                AND `od_pr` = `pr_id`
                AND UNIX_TIMESTAMP(`or_date`) >= '1262322000'
                AND UNIX_TIMESTAMP(`or_date`) <= '1346990399'
                AND (
                        `pr_id` = '415'
                        OR `pr_id` = '1088'
                        )
        GROUP BY `bgProducts`.`pr_id`

        UNION

        SELECT `bgProducts`.`pr_id`,
                `bgProducts`.`pr_sbtcode`,
                `bgProducts`.`pr_sdesc`,
                SUM(`od_quantity`) AS `od_quantity`,
                SUM(`od_amount`) AS `od_amount`,
                MIN(UNIX_TIMESTAMP(`or_date`)) AS `or_date`
        FROM `npOrderMain`
        INNER JOIN `npOrderData`
        INNER JOIN `bgProducts`
        WHERE `npOrderMain`.`or_id` = `npOrderData`.`or_id`
                AND `od_pr` = `pr_id`
                AND UNIX_TIMESTAMP(`or_date`) >= '1262322000'
                AND UNIX_TIMESTAMP(`or_date`) <= '1346990399'
                AND (
                        `pr_id` = '415'
                        OR `pr_id` = '1088'
                        )
        GROUP BY `bgProducts`.`pr_id`
        ) TEMPTABLE3
GROUP BY pr_id;

但这只是给了我这个:

+--------+--------------+----------------+---------------+------------+ 
| pr_id  |  pr_sbtcode  |  pr_sdesc      |  od_quantity  |  od_amount |
+--------+--------------+----------------+---------------+------------+ 
|   415  |  NP13        |  Product 13    |  5            |  125       | 
|  1088  |  NPAW        |  Product AW    |  4            |  100       |
+--------+--------------+----------------+---------------+------------+

我在这里想念什么?

4

1 回答 1

2

您的代码很难格式化,因为您添加了nbsp. 但无论如何试试这个,

第一次尝试

SELECT pr_id,
        pr_sbtcode,
        pr_sdesc,
        SUM(od_quantity) totalQuantity,
        SUM(od_amount) totalAmount
FROM 
    (
        SELECT `bgProducts`.`pr_id`,
                `bgProducts`.`pr_sbtcode`,
                `bgProducts`.`pr_sdesc`,
                SUM(`od_quantity`) AS `od_quantity`,
                SUM(`od_amount`) AS `od_amount`,
                MIN(UNIX_TIMESTAMP(`or_date`)) AS `or_date`
        FROM `bgOrderMain`
        INNER JOIN `bgOrderData`
        INNER JOIN `bgProducts`
        WHERE `bgOrderMain`.`or_id` = `bgOrderData`.`or_id`
                AND `od_pr` = `pr_id`
                AND UNIX_TIMESTAMP(`or_date`) >= '1262322000'
                AND UNIX_TIMESTAMP(`or_date`) <= '1346990399'
                AND (
                        `pr_id` = '415'
                        OR `pr_id` = '1088'
                    )
        GROUP BY `bgProducts`.`pr_id`
        UNION
        SELECT `bgProducts`.`pr_id`,
                `bgProducts`.`pr_sbtcode`,
                `bgProducts`.`pr_sdesc`,
                SUM(`od_quantity`) AS `od_quantity`,
                SUM(`od_amount`) AS `od_amount`,
                MIN(UNIX_TIMESTAMP(`or_date`)) AS `or_date`
        FROM `npOrderMain`
        INNER JOIN `npOrderData`
        INNER JOIN `bgProducts`
        WHERE `npOrderMain`.`or_id` = `npOrderData`.`or_id`
                AND `od_pr` = `pr_id`
                AND UNIX_TIMESTAMP(`or_date`) >= '1262322000'
                AND UNIX_TIMESTAMP(`or_date`) <= '1346990399'
                AND (
                        `pr_id` = '415'
                        OR `pr_id` = '1088'
                    )
        GROUP BY `bgProducts`.`pr_id`
    ) unionedTable
GROUP BY pr_id,
        pr_sbtcode,
        pr_sdesc

SQLFiddle 演示

于 2012-09-06T15:32:55.023 回答