SQL group_concat为什么我得到错误顺序的值?

时间:2017-02-19 14:36:42

标签: php sql json

为什么在使用group_concat并在json中解析它时,我的错误顺序是错误的?

这是我的代码

$sql = "select
          e_name,
          a_shortcut,
          GROUP_CONCAT(case
            when t_rank = 1 then  a_shortcut
            when t_rank = 2 then  a_shortcut
            when t_rank = 3 then  a_shortcut
          end separator ',') as group_con 
        from
          team inner join event on team.EID = event.eid Where e_type = 'nonsport'  group by event.eid";                         

         $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name); 

        $result = mysqli_query($con,$sql);


         $response = array();

        while($row=mysqli_fetch_array($result))
        {
        $ar=explode(',',$row['group_con']);
        array_push($response, array("e_name"=>$row[0],"First"=>$ar[0], 
        "Second"=>$ar[1], "Third"=>$ar[2]));

        } 
echo json_encode (array("nresults"=>$response));

这是给我的输出

    {"nresults":[

{"e_name":"AAA","First":"3rd","Second":"2nd","Third":"1st"},
{"e_name":"BBB","First":"2nd","Second":"1st","Third":"3rd"}

这是我的预期输出

    {"nresults":[

{"e_name":"AAA","First":"1st","Second":"2nd","Third":"3rd"},

{"e_name":"BBB","First":"1st","Second":"2nd","Third":"3rd"}

1 个答案:

答案 0 :(得分:0)

在执行order by之前从您获得a_shortcut的表格中进行选择时使用inner join