参考:http://www.cnblogs.com/h07061108/p/mysql_questions.html#3806338

实现如下效果

CREATE TABLE IF NOT EXISTS tb_amount(
`Id` INT NOT NULL AUTO_INCREMENT,
`Year` CHAR(4),
`Month` CHAR(2),
`Amount` DECIMAL(5,2),
PRIMARY KEY(`Id`)
); INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '1.1');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '1.2');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '1.3');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '1.4');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '2.1');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '2.2');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '2.3');
INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('', '', '2.4'); SELECT `Year`,
(SELECT Amount FROM tb_amount m WHERE `Month`=1 AND m.`Year`=tb_amount.`Year`) AS m1,
(SELECT Amount FROM tb_amount m WHERE `Month`=2 AND m.`Year`=tb_amount.`Year`) AS m2,
(SELECT Amount FROM tb_amount m WHERE `Month`=3 AND m.`Year`=tb_amount.`Year`) AS m3,
(SELECT Amount FROM tb_amount m WHERE `Month`=4 AND m.`Year`=tb_amount.`Year`) AS m4
FROM tb_amount GROUP BY `Year`; 或者
select year,
max(case month when 1 then amount else 0 end ) m1,
max(case month when 2 then amount else 0 end) m2,
max(case month when 3 then amount else 0 end) m3,
max(case month when 4 then amount else 0 end) m4
from tb_amount
group by year;

最新文章

  1. Android—基于Socket与上传图片到客户端
  2. Redmine 插件安装
  3. JavaScript中JSON的处理心得
  4. Oracle BIEE启停脚本
  5. Redis未授权访问漏洞分析
  6. POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
  7. goldengate 12c 针对oracle 12c配置的主要变化
  8. 转载: SQL Server中的索引
  9. 03_MySQL中文乱码处理_02_确保MySQL插入数据不乱码的解决方法
  10. codevs1380 没有丧尸的舞会
  11. 文档对象模型操作xml文档
  12. 重新理解一遍UpdatePanel
  13. 关于iPhone
  14. ie6下常见的bug 调整页面兼容性
  15. VMware10不能安装64位(linux)系统,提示此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
  16. 初识Velocity
  17. componentWillReceiveProps详解(this.props)状态改变检测机制
  18. String的split方法支持正则表达式
  19. TOJ3955: NKU ACM足球赛(并查集+map+细节题)
  20. YAML文件中在单一文件中区分多个文件

热门文章

  1. nextjs-demo
  2. OpenGL之位图的绘制和gluOrtho2D等函数详解
  3. PAT——1037. 在霍格沃茨找零钱
  4. Linux内存管理-高端内存(二)
  5. 随机获取UDID
  6. 对于PHP绘图技术的理解
  7. 小程序 组件 Component
  8. Storm相关笔记(包括Kafka和HBase)
  9. 学习tp5的第三天(模型)
  10. 利用RTTI实现Delphi的多播事件代理研究