2013-04-14 80 views
0

我有MySQL中的表,我想這樣的輸出:不同的值計數行

HNSHAK MOBILE COMPUTING =13 
HNSHAK STORAGE   =2 

產品組將分別我執行時間而改變。

表:

Productgroup Created By 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
STORAGE    HNSHAK 
MOBILE COMPUTING HNSHAK 
STORAGE    HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 
MOBILE COMPUTING HNSHAK 

回答

1

您的查詢應該是這樣的:使用GROUP BY Created_By, ProductGroup

SELECT `Created By`, ProductGroup, COUNT(*) `Count` 
FROM tablename 
GROUP BY `Created By`, ProductGroup; 

和聚合函數COUNT

這會給你:

Created By ProductGroup   Count 
HNSHAK  MOBILE COMPUTING  13 
HNSHAK  STORAGE     2 
0
SELECT `Created_By`, `Productgroup`, COUNT(`Productgroup`) 
FROM `table_name` 
GROUP BY `Productgroup` 

你應該在這裏使用GROUP BY。您還應該記住,如果Created By中的所有值都不相同(例如其中一個值爲COMPANY1),則要麼忽略Productgroup並選擇表中顯示的第一個值Productgroup,每個_manufacturer會有多行產品組。爲了實現後者,您將使用GROUP BY這種方式

GROUP BY `Created_By`, `Productgroup`