SQL查询给我错误的订单

时间:2017-02-20 10:24:34

标签: php mysql

为什么我没有通过此查询获得我想要的订单?

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 ORDER BY t_rank

当我输入t_rank时,此查询会给我一个随机顺序。它没有给我一个1,2,3的订单,而是它一直给我随机。有人可以帮我吗?

这是给我的结果

 {"nresults":[{"e_name":"Musical Festival - Song Composition","First":"2nd",
"Second":"1st",
"Third":"3rd"}]}

这是我的预期输出

    {"nresults":[{"e_name":"Musical Festival - Song Composition","First":"1st",
"Second":"2nd",
"Third":"3rd"}]}

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我现在就开始工作了。谢谢大家。

 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 order by t_rank separator ',') as group_con 
            from
              team inner join event on team.EID = event.eid Where e_type = 'nonsport' 
group by event.eid

我只是在group_concat

中的分隔符之前的结尾之后移动order by子句