我想到了这个查询-如何将多列的结果合并为1?

select
    R2.start_time,
    TIME_FORMAT(time(R2.start_time - time(10000)),
            '%H:%i:%s') as '-60min',
    TIME_FORMAT(time(R2.start_time - time(3000)),
            '%H:%i:%s') as '-30min',
    TIME_FORMAT(time(R2.start_time + time(10000)) - time(3000),
            '%H:%i:%s') as 30min,
    TIME_FORMAT(time(R2.start_time + time(10000)),
            '%H:%i:%s') as 60min
from
    (select
        rooms.id, rooms.number, rooms.building, rooms.capacity
    from
        rooms) R1,
    (select
        exam_schedules.room_id,
            exam_schedules.day,
            exam_schedules.start_time,
            exam_schedules.end_time
    from
        exam_schedules) R2
where
    R2.room_id = R1.id and R2.day = 'tuesday' AND R1.number = 006
group by 1


这是我的结果:(1行)

12:30:00    11:30:00    12:00:00    13:00:00    13:30:00


应该是(5行):

12:30:00
11:30:00
12:00:00
13:00:00
13:30:00

最佳答案

除了使用并集语句之外,还可以执行以下操作:

declare @dates table ( start_time datetime )

insert into @dates
select GETDATE()

declare @offsets table ( offset int, seq int)

insert into @values
select 0, 1
union select -60, 2
union select -30, 3
union select 30, 4
union select 60, 5

select * from @dates

select
    dateadd(minute, o.offset, r2.start_time) dt
from
    @dates r2, @offsets o
order by o.seq


由于我没有时间功能,因此无法测试您的查询,但最终会得到如下结果:

declare @offsets table ( offset int, seq int)

insert into @offsets
select 0, 1
union select -60, 2
union select -30, 3
union select 30, 4
union select 60, 5

select
    dateadd(minute, o.offset, t.start_time) dt
from
(
select
    R2.start_time as start_time
from
    (select
        rooms.id, rooms.number, rooms.building, rooms.capacity
    from
        rooms) R1,
    (select
        exam_schedules.room_id,
            exam_schedules.day,
            exam_schedules.start_time,
            exam_schedules.end_time
    from
        exam_schedules) R2
where
    R2.room_id = R1.id and R2.day = 'tuesday' AND R1.number = 006
group by 1
) t
, @offsets o
order by o.seq

07-27 21:28