早上好,我有这张叫“ soccer_team”的表

id  first_name  surname   player_number
1   Alexis      Sanchez   7
2   Petr        Cech      33
3   Hector      Bellerin  24
4   Olivier     Giroud    12
5   Theo        Walcott   14
6   Santi       Cazorla   19


如果我启动该命令,

SELECT CONCAT(first_name,' ',surname,' #',player_number) as 'i' from soccer_team order by player_number


它给了我

i
Alexis Sanchez #7
Olivier Giroud #12
Theo Walcott #14
Santi Cazorla #19
Hector Bellerin #24
Petr Cech #33


哪个是对的

但是当我跑步

SELECT GROUP_CONCAT(i,' ') as 'players'
FROM (SELECT CONCAT(first_name,' ',surname,' #',player_number) as 'i'
      from soccer_team
      order by player_number
     ) as a;


它给了我

players
Alexis Sanchez #7 ;Petr Cech #33 ;Hector Bellerin #24 ;Olivier Giroud #12 ;Theo Walcott #14 ;Santi Cazorla #19


虽然应该

players
Alexis Sanchez #7; Olivier Giroud #12; Theo Walcott #14; Santi Cazorla #19; Hector Bellerin #24; Petr Cech #33


**


  如何解决我所知道的问题,我想知道为什么会这样


**

更新


  我知道如何解决它,我感兴趣的是因为它可以在此解决
  办法

最佳答案

您可以在GROUP_CONCAT中指定顺序,前提是您要在子查询中包含player_number

SELECT GROUP_CONCAT(i ORDER BY player_number ASC SEPARATOR ' ') as 'players'
FROM (
SELECT CONCAT(first_name,' ',surname,' #',player_number) as 'i' ,player_number
from soccer_team
) as a;


这是Demo

实际上,您可以通过删除子查询来简化操作,该子查询仍将为您提供相同的结果

SELECT GROUP_CONCAT(CONCAT(first_name,' ',surname,' #',player_number)
                     ORDER BY player_number ASC SEPARATOR ' ') as 'players'
FROM soccerteam


这是Demo

关于mysql - mysql子查询不保留命令为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48905908/

10-16 13:09