Table
ID 名称
1 Akhil
2 Akira
3 Anuj
4 Bia
5 Bina
6 Chetu
7 Chini
8 Chikna
9 Chana
输出
ID 名称
1 Akhil
2 Akira
3 Bia
4 Bina
6 Chetu
7 Chini
答案 0 :(得分:0)
这里是查询。在Result中有更多字段,但您可以删除它们。他们只是看看查询是如何工作的
SELECT * FROM (
SELECT
@nr := IF(@old_val = SUBSTRING(t.name,1,1),(@nr +1),1) AS nr,
@old_val:=SUBSTRING(t.name,1,1) AS tmp,
t.name FROM mytable t,
(SELECT @nr:=0, @old_val:='') AS TMP
ORDER BY NAME
) AS result WHERE nr < 3;
请告诉我它是否适合您