简体   繁体   中英

how to count row group in select mysql?

hj all, help me!! I use MYSQL, i have a table with data:

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

i want a query mysql (or function) select return result:

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

STT is number str appears in table thanks!

Just Try this

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM