2015-02-09 85 views
-1

的數據/總和我有以下查詢這是通過一些命令在一個數據庫中提取:MySQL的 - 合併總

SELECT company, amount, total FROM cscart_order_details 
INNER JOIN cscart_orders 
ON cscart_order_details.order_id = cscart_orders.order_id 
WHERE 
    date(FROM_UNIXTIME(`timestamp`)) >= ADDDATE(DATE(NOW()), INTERVAL -6 DAY) 

這將產生以下信息:

|company | | amount | | total | 
================================ 
| NTE001 | | 1 | | 51.06 | 
| NTE001 | | 1 | | 126.76 | 
| NTE001 | | 1 | | 126.76 | 
| EUR004 | | 1 | | 832.13 | 
| EUR004 | | 1 | | 832.13 | 
| EUR004 | | 1 | | 832.13 | 
| EUR004 | | 1 | | 231.37 | 
| EUR004 | | 1 | | 231.37 | 
| EUR004 | | 2 | | 263.92 | 
| EUR004 | | 1 | | 131.96 | 
| B&T001 | | 1 | | 929.83 | 
| B&T001 | | 1 | | 929.83 | 

我該如何去關於聯合線?例如,我想提出的同一公司的所有行在一起,就像這個例子:

| NTE001 | | 3 | | 304.58 | 
| EUR004 | | 7 | | 3223.05 | 
| B&T001 | | 2 | | 1859.66 | 

編輯:如果你是反對投票,請讓我知道我怎麼能改善這個問題!

+0

SELECT公司,SUM(數量),SUM(總)FROM(此處查詢)X GROUP BY公司 – Strawberry 2015-02-09 17:21:48

回答

0

所以你想要的東西,如:

SELECT company, 
      count(amount) amount, 
      sum (total) total 
     FROM cscart_order_details 
INNER JOIN cscart_orders 
     ON cscart_order_details.order_id = cscart_orders.order_id 
    WHERE date(FROM_UNIXTIME(`timestamp`)) >= ADDDATE(DATE(NOW()), INTERVAL -6 DAY) 
    GROUP BY company