2014-03-31 57 views
1

hj all,help me !! 我用MYSQL,我有一個表的數據:如何在select mysql中統計行組?

|id | str | 
    ------------- 
    1  aabb 
    2  aaac 
    3  aaad 
    4  aabb 
    5  aabb 
    6  aaac 

我想查詢的MySQL(或功能)選擇返回的結果:

 STT | str | 
     1  aabb 
     1  aaac 
     1  aaad 
     2  aabb 
     3  aabb 
     2  aaac 

STT是數海峽出現在表 感謝!

+0

正是你想要的.. – jmail

回答

1

只是嘗試這種

SELECT A.id, A.str, count(*) AS STT 
FROM Table1 A 
JOIN Table1 B ON A.str = B.str AND A.id >= B.id 
GROUP BY A.id, A.str 

Fiddle Demo

輸出:

ID STR STT 
1 aabb 1 
2 aaac 1 
3 aaad 1 
4 aabb 2 
5 aabb 3 
6 aaac 2