mysql子查询不保持顺序为什么?

时间:2018-02-21 12:25:34

标签: mysql sql

早上好,我把这张桌子命名为' 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

**

  

如何解决我所知道的问题,我想知道为什么会发生这种情况

**

更新

  

我知道如何解决它,我感兴趣的是因为它在这方面起作用   方式

1 个答案:

答案 0 :(得分:1)

如果您在子查询中包含{ "name": ["This field is required."] } ,则可以在{ "username": ["This field is required."] } 内指定订单。

GROUP_CONCAT

这是Demo

实际上,您可以通过删除仍然会给您相同结果的子查询来简化

player_number

这是Demo