如何在mysql

时间:2016-12-13 03:34:55

标签: mysql

如何仅使用1个表选择1列两次,问题是我必须将1列分成2个返回

ex:table Logs

TIMELOGS   11:00 , 12:12 , 13:00 , 17:01 , 17:05 , 17:10

TIMEMODE     0    ,   0     ,  0    ,   1    ,   1     ,   1

输出应为

IN   11:00 , 12:12 , 13:00

OUT   17:01, 17:05 , 17:10

如何将这些与查询结合起来

Select TIMELOGS as IN FROM table_logs where TIMEMODE = 0;

Select TIMELOGS as OUT FROM table_logs where TIMEMODE = 1; 

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你想要枚举0和1然后分组:

select max(case when mode = 0 then time end),
       max(case when mode = 1 then time end)
from (select l.*,
             (@rn := if(@m = mode, @rn + 1,
                        if(@m := mode, 1, 1)
                       )
             ) as rn
      from logs l cross join
           (select @rn := 0, @m := -1) params
      order by mode, time
     ) l
group by rn;

答案 1 :(得分:0)

要输出您描述的格式(假设您正在使用MySQL),您可以这样做:

select 
  case when TIMEMODE = 0 then 'IN' else 'OUT' end,
  group_concat(TIMELOGS)
from table_logs
group by TIMEMODE