당신이 order
및 item
두 값의 알려진 숫자를 가지고 있다면, 당신은 하드에 쿼리를 코딩 할 수 있습니다 :
select id,
max(case when `order` = 1 then data end) order1,
max(case when `order` = 2 then data end) order2,
max(case when `order` = 3 then data end) order3,
max(case when item = 1 then price end) item1,
max(case when item = 2 then price end) item2,
max(case when item = 3 then price end) item3,
max(case when item = 4 then price end) item4
from tableA
group by id;
이 Demo를 참조하십시오. 그러나 문제의 일부는 여러 데이터 열을 변환하려고하기 때문에 발생합니다. 최종 결과를 얻으려는 나의 제안은 데이터를 먼저 unpivot하는 것입니다. MySQL에는 unpivot 함수가 없지만 UNION ALL을 사용하여 여러 쌍의 열을 행으로 변환 할 수 있습니다. UNPIVOT에 대한 코드는 다음과 유사합니다
select id, concat('order', `order`) col, data value
from tableA
union all
select id, concat('item', item) col, price value
from tableA;
는 Demo를 참조하십시오. 이것의 결과는 다음과 같습니다
| ID | COL | VALUE |
-----------------------
| 1 | order1 | P |
| 1 | order1 | P |
| 1 | order1 | P |
| 1 | item1 | 50 |
| 1 | item2 | 60 |
| 1 | item3 | 70 |
이이 order
/data
및 item
/price
의 여러 열을 취한보고 여러 행으로 변환 할 수있다. 그이 완료되면, 당신은 CASE로 집계 함수를 사용하여 열을 다시 값을 변환 할 수 있습니다
select id,
max(case when col = 'order1' then value end) order1,
max(case when col = 'order2' then value end) order2,
max(case when col = 'order3' then value end) order3,
max(case when col = 'item1' then value end) item1,
max(case when col = 'item2' then value end) item2,
max(case when col = 'item3' then value end) item3
from
(
select id, concat('order', `order`) col, data value
from tableA
union all
select id, concat('item', item) col, price value
from tableA
) d
group by id;
이 Demo를 참조하십시오. 마지막으로, 당신은 동적 준비된 문 쿼리에 위의 코드를 변환 할 필요가 :
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when col = ''',
col,
''' then value end) as `',
col, '`')
) INTO @sql
FROM
(
select concat('order', `order`) col
from tableA
union all
select concat('item', `item`) col
from tableA
)d;
SET @sql = CONCAT('SELECT id, ', @sql, '
from
(
select id, concat(''order'', `order`) col, data value
from tableA
union all
select id, concat(''item'', item) col, price value
from tableA
) d
group by id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
이 SQL Fiddle with demo를 참조하십시오. 결과는 다음과 같습니다.
| ID | ORDER1 | ORDER2 | ORDER3 | ITEM1 | ITEM2 | ITEM3 | ITEM4 |
-------------------------------------------------------------------
| 1 | P | Q | (null) | 50 | 60 | 70 | (null) |
| 2 | P | (null) | S | 50 | 60 | (null) | 80 |