2012-09-06 132 views
0

試圖將兩個查詢的結果組合在一起。當我運行此查詢:如何在mysql UNION查詢中寫入GROUP BY

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  | 
+--------+--------------+----------------+---------------+------------+ 

我想,結合那些到2號線的結果是什麼:

+--------+--------------+----------------+---------------+------------+ 
| 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  | 
+--------+--------------+----------------+---------------+------------+ 

我在這裏失蹤?

+2

WEW做了吳宇森編輯:) –

+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 Demo

+0

工作正常!非常感謝!哎呀,知道這會變得簡單,我只是沒有看到。不知道nbsp空間來自哪裏,因爲我沒有故意將它們添加到... – user1652368

+0

歡迎您@ user1652368!只是不要忘記接受答案,如果你覺得它真的解決了你的問題:) –