2012-12-13 39 views
0

我得到了下面的MySQL表MySQL查詢數

PrefixID + Prefix 
001  + 110 
005  + 550 
005  + 550 
005  + 550 
005  + 550 

,我希望把它變成下表

PrefixID + Prefix + Count 
001  + 110 + 1 
005  + 550 + 4 

計數是特定的前綴 我怎樣才能實現的只是頻率那?

回答

2

使用GROUP BY子句和COUNT()骨料:

SELECT `PrefixID` 
    , `Prefix` 
    , COUNT(1) AS `Count` 
    FROM mytable 
GROUP BY `PrefixID`, `Prefix` 

從你的樣本數據,似乎在PrefixPrefixID取決於你可能需要調整查詢,這取決於(一一對應)。如果您想確保PrefixIDPrefix在結果集中是唯一的。

+0

感謝,它完美匹配的要求 – yas

1

試試這個:

SELECT 
    PrefixID, 
    MAX(Prefix) as Prefix, 
    Count(Prefix) as Count 
FROM table 
GROUP BY PrefixID 
1

試試這個:

Select 
PrefixID, Prefix, count(Prefix) 
from table 
group by Prefix