2012-04-21 4 views
0

이 쿼리는 여러 열의 결과를 1로 결합하려면 어떻게해야합니까? (1 행)여러 열을 하나로 결합

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

(5 행)해야한다 :

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 

내 결과를 제외하고 여기에 노동 조합 문의 활용에서

12:30:00 
11:30:00 
12:00:00 
13:00:00 
13:30:00 
+0

1 열 x 1 행 또는 1 열 x 5 행 – shashankaholic

+0

죄송합니다 - 5 rows – Andrew

+1

결과를 UNPIVOT으로 찾고 있습니다. mysql에서 이것을 수행하는 지원 기능이 없지만, 내가 아는 방법입니다 이렇게하려면 우아하지 않습니다.'SELECT ccolumn1 from yourQuery UNION SELECT column2 from yourQuery .... ' –

답변

1

무언가의 예 당신은 할 수있다 :

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 

Ca NT 정말 시간 기능을 가지고 있지 않기 때문에 쿼리를 테스트하지만,이 같은 끝낼 것 : 당신이 SQL에 너무 많은 기능을 넣어 위해 노력하고 같은

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 
+0

오프셋 테이블 변수를 적절한 테이블로 돌리면이 속도를 조금 더 높일 수 있다고 말하는 걸 잊어 버리십시오. –

-1

는 소리. 시작 시간을 선택하고 프로그래밍에서 전후 시간을 추가하십시오. SQL은 프로그래밍 언어가 아닌 쿼리 언어입니다. 열에는 다음 고정되어있는 경우

0

당신은 내가이 작업을 수행하는 가장 좋은 방법이 아니다 생각이 하나

SELECT col1 FROM yourtable UNION SELECT col2 FROM yourtable 

을 시도하지하지만 당신을 도울 수 있기를 바랍니다 수 있습니다.

관련 문제