2013-05-21 8 views
0

나는 MySQL 데이터베이스를 가지고 있습니다. 다른 개발자가 제안한 예상 프로 시저 목록과 예상 목록이 들어있는 테이블이 있습니다.MySQL group_concat 쿼리 도움이 필요합니다.

는 다음과 같이 사용자가 proc_name에 대한 모든 시간을 게시하지 않은 경우

proc_name user_id est_time 
------------------------------- 
'A'   1   20 
'A'   3   15 
'B'   2   16 
'B'   4   18 
'C'   1   20 

지금 내가이

A|1_20|2_0 |3_15|4_0 
B|1_20|2_16|3_0 |4_18 
C|1_20|2_0 |3_0 |4_0 

같은 출력 뭔가가 필요 '0'인쇄됩니다.

도움이 될 것입니다.

답변

0

"group_concat"이 아닙니다. 이것은 피벗의 예입니다.

select proc_name, 
     concat('1_', max(case when user_id = 1 then est_time else 0 end)), 
     concat('2_', max(case when user_id = 2 then est_time else 0 end)), 
     concat('3_', max(case when user_id = 3 then est_time else 0 end)), 
     concat('4_', max(case when user_id = 4 then est_time else 0 end)) 
from t 
group by proc_name 

는 당신이 첫 번째 열에 모든 사용자 1 개의 퍼팅과 또한 열 값의은 "1"을 가하고 있다고하지만, 그것은 이상한 찾을 수 있습니다. 당신이 정말로 하나의 문자열로 데이터를 원하는 경우

사실, 당신은 별도의 열을 가하고보다는 함께 위의 결과를 연결할 수 있습니다 :

select concat_ws('|', proc_name, 
       concat('1_', max(case when user_id = 1 then est_time else 0 end)), 
       concat('2_', max(case when user_id = 2 then est_time else 0 end)), 
       concat('3_', max(case when user_id = 3 then est_time else 0 end)), 
       concat('4_', max(case when user_id = 4 then est_time else 0 end)) 
       ) 
from t 
group by proc_name 
+0

감사를 많이 고든. 나는 ..... 문제를 해결했다 : D –

관련 문제