如何仅使用 1 个表选择 1 列两次,问题是我必须将 1 列分成 2 个返回
例如:表日志
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;
最佳答案
如果我理解正确的话,您想要枚举 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;
关于mysql - 如何在mysql中两次选择1个表中的同一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113330/