mysql - 将组信息显示为表字段

时间:2015-09-08 09:39:21

标签: mysql pivot

我有一张桌子' operation_record'像这样:

日期操作
1-1 a 1-1 b
1-1 b
1-1 a 1-1 a 1-1 c
1-1 a 1-2 c
1-2 b
1-2 c
1-2 a 1-2 a 1-2 b
1-2 c

我想要的是这样的:

data operation_a operation_b operation_c
1-1 4 2 1
1-2 2 2 3

如果操作是固定的,我可以使用这样的东西:

select o.uc, ,a.rc operaiton_a, b.rc operaiton_b, c.rc operaiton_c from
(select date uc,count(1) rc from operaion_record group by date) o 
left join 
(select date uc,count(1) rc from operaion_record where operation='a' group by date)a on o.uc=a.uc
left join 
(select date uc,count(1) rc from operaion_record where operation='b' group by date)b on o.uc=b.uc
left join 
(select date uc,count(1) rc from operaion_record where operation='c' group by date)b on o.uc=c.uc

但问题是,操作可能会有所不同。

但是,我可以通过

获得这些操作
select distinct(operation) from operation_record

然后,我如何将这些值作为新列并获得这些计数?

0 个答案:

没有答案