网上的都是一些静态的,用CASE WHEN结构实现。所以我写了一个动态的。

SP 代码:

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`sp_row_column_wrap`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_row_column_wrap`(IN $schema_name varchar(64),
IN $table_name varchar(64))
BEGIN
  declare cnt int(11);
  declare $table_rows int(11);
  declare i int(11);
  declare j int(11);
  declare s int(11);
  declare str varchar(255);
  -- Get the column number of the table
  select count(1) from information_schema.columns where table_schema=$schema_name and table_name=$table_name into cnt;
  -- Get the row number of the table
  select table_rows from information_schema.tables where table_schema = $schema_name and table_name=$table_name into $table_rows;
  -- Check whether the table exists or not
  drop table if exists test.temp;
  create table if not exists test.temp (`1` varchar(255) not null);
  -- loop1 start
  set i = 0;
  loop1:loop
    if i = $table_rows-1 then
      leave loop1;
    end if;
    set @stmt1 = concat('alter table test.temp add `',i+2,'` varchar(255) not null');
    prepare s1 from @stmt1;
    execute s1;
    deallocate prepare s1;
    set @stmt1 = '';
    set i = i + 1;
  end loop loop1;
  -- loop1 end;
  set s = 0;
  -- loop2 start
  loop2:loop
  -- leave loop2
    if s=cnt then
      leave loop2;
    end if;
    set @stmt2 = concat('select column_name from information_schema.columns where table_schema="',$schema_name,
                        '" and table_name="',$table_name,'" limit ',s,',1 into @temp;');
    prepare s2 from @stmt2;
    execute s2;
    deallocate prepare s2;
    set @stmt2 = '';
    set j=0;
    set str = ' select ';
    -- Loop3 start
    loop3:loop
      if j = $table_rows then
        leave loop3;
      end if;
      set @stmt3 = concat('select ',@temp,' from ',$schema_name,'.',$table_name,' limit ',j,',1 into @temp2;');
      prepare s3 from @stmt3;
      execute s3;
      set str = concat(str,'"',@temp2,'"',',');
      deallocate prepare s3;
      set @stmt3 = '';
      set j = j+1;
    end loop loop3;
    set str = left(str,length(str)-1);
    -- insert new data into table
    set @stmt4 = concat('insert into test.temp',str,';');
    prepare s4 from @stmt4;
    execute s4;
    deallocate prepare s4;
    set @stmt4 = '';
    set s=s+1;
  end loop loop2;
END$$

DELIMITER ;

以下是测试结果:
======
select * from a;
select * from b;
select * from salary;

call sp_row_column_wrap('test','a');
select * from test.temp;
call sp_row_column_wrap('test','b');
select * from test.temp;
call sp_row_column_wrap('test','salary');
select * from test.temp;

query result(2 records)

aid title
1 111
2 222

query result(3 records)

bid aid image time
1 2 1.gif 2007-08-08
2 2 2.gif 2007-08-09
3 2 3.gif 2007-08-08

query result(7 records)

id cost des Autoid
1 10 aaaa 1
1 15 bbbb 2
1 20 cccc 3
2 80 aaaa 4
2 100 bbbb 5
2 60 dddd 6
3 500 dddd 7

query result(2 records)

1 2
1 2
111 222

query result(4 records)

1 2 3
1 2 3
2 2 2
1.gif 2.gif 3.gif
2007-08-08 2007-08-09 2007-08-08

query result(4 records)

1 2 3 4 5 6 7
1 1 1 2 2 2 3
10 15 20 80 100 60 500
aaaa bbbb cccc aaaa bbbb dddd dddd
1 2 3 4 5 6 7

转载于:https://www.cnblogs.com/secbook/archive/2008/04/19/2655306.html

MySQL动态行转列相关推荐

  1. MySQL 动态 行转列

    MySQL 动态 行转列 1.需求 2.建表 3.插入数据 4. 转换前结果 5. 动态转换 1.需求 在每一行的内容不确定的情况下,需要动态的把行转为列. 2.建表 DROP TABLE IF EX ...

  2. mysql动态行转列函数_[MSSQL]采用pivot函数实现动态行转列

    环境要求:2005+ 在日常需求中经常会有行转列的事情需求处理,如果不是动态的行,那么我们可以采取case when 罗列处理. 在sql 2005以前处理动态行或列的时候,通常采用拼接字符串的方法处 ...

  3. MySQL 动态行转列

    CREATE TABLE `tbl01` (   `id` INT(11) DEFAULT NULL,   `kemu` VARCHAR(20) COLLATE utf8_unicode_ci DEF ...

  4. mysql实现动态行转列

    需求背景:在任务管理系统中,有任务详情表,每个任务下又分子任务节点,每个任务节点都有具体的跟进日期,包括开始时间结束时间,每天的任务完成进度. 有这样一个需求:在任务管理系统中根据任务节点id,展示每 ...

  5. mysql中将列动态转换为行,mysql 行转列 MySQL数据库动态行转列

    想把mysql一个表的行转成列,图1是原表,想实现图2的样式SELECT MAX(CAS就是一个动态的行列转换 CREATE TABLE `c_wssb_zz` ( `aa011` varchar(1 ...

  6. mysql行转列sql函数_sql动态行转列的两种方法

    第一种方法: 代码如下: select *from ( select Url,case  when  Month=01 then  '1月' when  Month=02 then '2月' when ...

  7. 动态行转列:处理不确定数量的行转列操作

    目录 介绍 分析过程 数据样例 开始动手 添加辅助列 全连接换左连接 完成静态SQL 将动态部分设置到变量 改编为动态SQL 验证 总结 介绍 行转列操作是一种常见的数据转换技术,它可以将原始的行数据 ...

  8. mssql 动态行转列。

    mssql 动态行转列. create table #a (a int , b char(4)) insert into #a select 1,'张三' insert into #a select ...

  9. 关于mysql的行转列问题

    关于mysql的行转列问题 一张表,里面有  id  name   state    customerid 4个字段,其中status有3个值0 1 2 ,用一条sql查询出此种格式 customer ...

最新文章

  1. 接口与抽象类的使用选择
  2. 搜狗手机输入法php,在线调用搜狗云输入法
  3. 去哪儿网消息队列设计与实现
  4. Windows CE 程序设计 (3rd 版)
  5. ITK:跳过特定区域时在图像上迭代
  6. .NET(C#)有哪些主流的ORM框架
  7. 面试题:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序...
  8. python爬虫有道词典_Python爬取有道词典,有道的反爬很难吗?也就这样啊!
  9. Vue-图片切换实例
  10. 今天开始用 VSU 2010
  11. 快速查找对方IP地址经典技巧汇总
  12. 智能驾驶LQR横向控制算法
  13. 什么是拖库,撞库和洗库
  14. mysql 表的详细_MySQL的库表详细操作
  15. 【Bzoj2242】计算器
  16. 搭建 Nexus 私服
  17. 2022-2027年中国闪速存储器集成电路行业市场深度分析及发展战略规划报告
  18. 施工控制网的精度确定方法?
  19. Redis 源码解读之 Rehash 的调用时机
  20. 玩转华为ENSP模拟器系列 | 配置普通NTP对等体模式示例

热门文章

  1. 1.1图像处理的概念
  2. NOIP模拟——change
  3. kalman滤波器公式的推导
  4. yii2_getDb()自动切换数据库
  5. imx6q 开发板_mplayer移植-迅为IMX6Q开发板
  6. 结巴分词优点_中文分词概述及结巴分词原理
  7. 百度地图API : 修改marker图标(icon)
  8. ElementUI如何将当前组件的所有属性快速传递给子组件
  9. kotlin android获取按钮,Kotlin 实现按钮点击跳转监听事件方式
  10. springboot+sockjs进行消息推送(广播模式)