【问题标题】:SQL group_concat why am i getting values in wrong order?SQL group_concat 为什么我以错误的顺序获取值?
【发布时间】:2017-02-19 14:36:42
【问题描述】:

为什么我在使用 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"}

【问题讨论】:

  • 你应该格式化你的结果,这样你想要的顺序就很清楚了。
  • 在数据库中先生?
  • 在执行 inner join 之前从获取 a_shortcut 的表中选择时使用 order by
  • @ultrajohn 先生,您能回答一下吗?所以我可以接受它
  • @orange,当然。就在那里。

标签: php sql json


【解决方案1】:

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

【讨论】:

  • 警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在 C:\xampp\htdocs\atfest\cpanel\mobile\NonSportsResult.php 第 22 行 {"nresults":[]}
  • 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 order by t_rank team inner join event on team.EID = event.eid 其中 e_type = 'nonsport' group by event.eid
  • 张贴我的sql查询先生
  • 我知道你能发布正确的,先生,似乎找不到错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 2014-12-02
相关资源
最近更新 更多