अगर आप कोशिश करते हैं:
SET @name:='',@num:=0;
SELECT id,
@num:= if(@name = user, @num, @num + 1) as number,
@name := user as user
FROM foo
ORDER BY id ASC;
यह देता है:
+------+--------+------+
| id | number | user |
+------+--------+------+
| 1 | 1 | a |
| 2 | 1 | a |
| 3 | 1 | a |
| 4 | 2 | b |
| 5 | 2 | b |
| 6 | 2 | b |
| 7 | 3 | a |
| 8 | 3 | a |
+------+--------+------+
तो फिर आप कोशिश कर सकते हैं:
SET @name:='',@num:=0;
SELECT COUNT(*) as count, user
FROM (
SELECT @num:= if(@name = user, @num, @num + 1) as number,
@name := user as user
FROM foo
ORDER BY id ASC
) x
GROUP BY number;
जो देता है
+-------+------+
| count | user |
+-------+------+
| 3 | a |
| 3 | b |
| 2 | a |
+-------+------+
(मैंने अपनी टेबल foo
. को कॉल किया और सिर्फ इस्तेमाल किए गए नाम a
और b
क्योंकि मैं zhangsan
write लिखने में बहुत आलसी था और lisi
बार-बार)।