Mysql 列转行

SQL 开发案例

Mysql 列转行

第一篇 试着 发一下

表结构
create table t1 (
day varchar(8),
registercount int,
logincount int
)


插入语句
insert into t1 values ('20120101' ,1,4 ) ;
insert into t1 values ('20120102' ,2,5 ) ;
insert into t1 values ('20120103' ,3,6 ) ;

Current database: test

+----------+---------------+------------+-----+
| day      | registercount | logincount | 1   |
+----------+---------------+------------+-----+
| 20120101 |             1 |          4 | 1   |
| 20120101 |             1 |          4 | 2   |
| 20120102 |             2 |          5 | 1   |
| 20120102 |             2 |          5 | 2   |
| 20120103 |             3 |          6 | 1   |
| 20120103 |             3 |          6 | 2   |
+----------+---------------+------------+----+

List item

6 rows in set (0.01 sec)


select  case when t.a = '1' then 'registercount' when  t.a = '2' then 'logincount' end  type , min(case when t1.day = '20120101' and t.t.a = '1'   then t1.registercount   when t1.day = '20120101' and t.t.a = '2'   then  t1.logincount end )   d20120101
,min(case when t1.day = '20120102' and t.t.a = '1'   then t1.registercount
when t1.day = '20120102' and t.t.a = '2'   then  t1.logincount end)   d20120102
,min(case when t1.day = '20120103' and t.t.a = '1'   then t1.registercount
when t1.day = '20120103' and t.t.a = '2'   then  t1.logincount end )  d20120103     
                     from t1 ,(select 1 a union all select 2 ) t
group by case when t.a = '1' then 'registercount' when  t.a = '1' then 'logincount' end
;

我是知数堂SQL 优化班老师~ ^^
如有关于SQL优化方面疑问和一起交流的请加 并且 @兔子@知数堂SQL优化
高性能MySQL,SQL优化群 有叶金荣,吴炳锡 两位大神坐镇 : 579036588
欢迎加入 知数堂大家庭

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值