我在不同市場有多個產品銷售,我需要總結每個標題的總計。要啓動:使用GROUP BY語句合計行
mysql> SELECT title, partner_share_currency, us_earnings_usd, cad_earnings_cad
FROM raw_financials WHERE title LIKE "%Gamers%";
+--------+------------------------+-----------------+------------------+
| title | partner_share_currency | us_earnings_usd | cad_earnings_cad |
+--------+------------------------+-----------------+------------------+
| Gamers | USD | 3.2500 | 0.0000 |
| Gamers | CAD | 0.0000 | 4.0000 |
| Gamers | USD | 4.5000 | 0.0000 |
+--------+------------------------+-----------------+------------------+
這是我目前正在做得到GROUP BY標題:
mysql> SELECT title, us_earnings_usd, cad_earnings_cad
FROM raw_financials WHERE title LIKE "%Gamers%" GROUP BY title;
+--------+-----------------+------------------+
| title | us_earnings_usd | cad_earnings_cad |
+--------+-----------------+------------------+
| Gamers | 3.2500 | 0.0000 |
+--------+-----------------+------------------+
正如你所看到的,它不求和值的行。我將如何改變SELECT語句,它總結了值的行,給我:
+--------+-----------------+------------------+
| title | us_earnings_usd | cad_earnings_cad |
+--------+-----------------+------------------+
| Gamers | 7.7500 | 4.0000 |
+--------+-----------------+------------------+
'group by'子句不做求和。他們只是說哪些行應該被「摺疊」以用於聚合函數。你仍然必須告訴'sum()'函數(或其他聚合函數)應該應用於哪些字段。 – 2012-01-16 20:01:01