SQL - 从俱乐部名称中选择一个排序,因此没有4个相邻的行具有相同的俱乐部名称

时间:2015-07-25 14:36:26

标签: mysql

当我尝试使用mysql命令从数据库中获取结果时,我得到了以下结果:

从y_of_birth = 2000

的玩家中选择full_name,club_name
+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| Mohamed gad  |   El Ahly        |
+----+------+--+------------------+
| ehab zeyad   |   El Ahly        |
+----+---------+------------------+
| moaz maged   |   El Ahly        |
+--------------+------------------+
| mazen mahmod |   El Ahly        |
+--------------+------------------+
| ahmed shawky |   Petroget       |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+--------------+------------------+
| gamael zayed |   Petroget       |
+--------------+------------------+
| mano mazen   |   Petroget       |
+--------------+------------------+
| shady srour  |   Petroget       |
+--------------+------------------+
| mamdouh gamal|   Petroget       |
+--------------+------------------+
| khaled zein  |   Petroget       |
+--------------+------------------+
| shady sameer |   Petroget       |
+--------------+------------------+
| wahed mahmoud|   Petroget       |
+--------------+------------------+
| samy shahen  |   Petroget       |
+----+------+--+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| hassan maged |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| zakareya gad |   Cocorico       |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| mazen kamal  |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| mesho kareem |   Cocorico       |
+--------------+------------------+
| kamal mado   |   Cocorico       |
+--------------+------------------+
| hossam magdy |   Cocorico       |
+--------------+------------------+
| sayed ammar  |   Cocorico       |
+--------------+------------------+
| karem gad    |   Cocorico       |
+--------------+------------------+

但我希望得到所需的输出:

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| kamal mado   |   Cocorico       |
+--------------+------------------+
| hossam magdy |   Cocorico       |
+--------------+------------------+
| sayed ammar  |   Cocorico       |
+--------------+------------------+
| karem gad    |   Cocorico       |
+--------------+------------------+
| Mohamed gad  |   El Ahly        |
+----+------+--+------------------+
| ehab zeyad   |   El Ahly        |
+----+---------+------------------+
| moaz maged   |   El Ahly        |
+--------------+------------------+
| mazen mahmod |   El Ahly        |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+--------------+------------------+
| gamael zayed |   Petroget       |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| mazen kamal  |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| mesho kareem |   Cocorico       |
+--------------+------------------+
| mano mazen   |   Petroget       |
+--------------+------------------+
| shady srour  |   Petroget       |
+--------------+------------------+
| mamdouh gamal|   Petroget       |
+--------------+------------------+
| khaled zein  |   Petroget       |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| hassan maged |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| zakareya gad |   Cocorico       |
+--------------+------------------+
| shady sameer |   Petroget       |
+--------------+------------------+
| wahed mahmoud|   Petroget       |
+--------------+------------------+
| samy shahen  |   Petroget       |
+----+------+--+------------------+
| abdo samy    |   Petroget       |
+----+------+--+------------------+
 and so on if more 
 .......

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

假设俱乐部拥有相似数量的玩家,您可以枚举行并基本按顺序排序:

select p.*
from (select p.*,
             (@rn := if(@c = club_name, @rn + 1,
                        if(@c := club_name, 1, 1)
                       )
             ) as seqnum
      from players p cross join
           (select @c := '', @rn := 0) params
      order by club_name
     ) p
order by floor((seqnum - 1) / 4), club_name