问题描述:

Table

Id name

1 Akhil

2 Akira

3 Anuj

4 Bia

5 Bina

6 Chetu

7 Chini

8 Chikna

9 Chana


Output

Id name

1 Akhil

2 Akira

3 Bia

4 Bina

6 Chetu

7 Chini

网友答案:

Here the Query. In the Result are more field, but you can remove them. They are only to see how the Query works

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;

Please let me know if it works for you

相关阅读:
Top