douyueqing1530 2017-02-12 17:18
浏览 32
已采纳

GROUP BY ENUM来汇编PHP

I have some sql table table

+----+------+--------+------------+------------+
| id | code | name   | Instructor | PlanePilot |
+----+------+--------+------------+------------+
|  1 |  001 | sasha  | N          | N          |
|  2 |  002 | sasha2 | Y          | N          |
|  3 |  003 | sasha3 | N          | Y          |
|  4 |  004 | sasha4 | Y          | Y          |
|  5 |  005 | sasha5 | Y          | Y          |
|  6 |  006 | sasha6 | N          | N          |
|  7 |  007 | sasha7 | Y          | N          |
|  8 |  008 | sasha8 | Y          | N          |
+----+------+--------+------------+------------+

I want to sort all of it to PHP Array with GROUP by ( Instructor , PlanePilot )

I results I want to get some php array like:

$array =[
withoutInstructorPLANEPilot:[sasha,sasha6],
Instructor:[sasha2,sasha4,sasha5,sasha7,sasha8],
PlanePilot:[sasha3,sasha4,sasha5]
]

How do it without 3 select queries like :

SELECT * FROM mytable where Instructor= 'Y'

I want to do it with 1 queries and convert it to PHP array

  • 写回答

1条回答 默认 最新

  • dtgsl60240 2017-02-12 17:21
    关注

    Is this what you want?

    select group_concat( (case when Instructor = 'N' and PlanePilot = 'N' then name end) ) as neither,
           group_concat( (case when Instructor = 'Y' then name end) ) as instructors,
           group_concat( (case when PlanePilot = 'Y' then name end) ) as planepilots
    from t;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码