随笔 - 108  文章 - 2  评论 - 1  阅读 - 74139

mysql 经典题目

题目1:实现如下效果

复制代码
 1 CREATE TABLE IF NOT EXISTS tb_amount(
 2    `Id` INT NOT NULL AUTO_INCREMENT,
 3    `Year` CHAR(4),
 4    `Month` CHAR(2),
 5    `Amount` DECIMAL(5,2),
 6    PRIMARY KEY(`Id`)
 7 );
 8 
 9 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1991', '1', '1.1');
10 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1991', '2', '1.2');
11 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1991', '3', '1.3');
12 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1991', '4', '1.4');
13 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1992', '1', '2.1');
14 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1992', '2', '2.2');
15 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1992', '3', '2.3');
16 INSERT INTO `tb_amount`(`Year`, `Month`, `Amount`) VALUES('1992', '4', '2.4');
17 
18 SELECT `Year`,
19 (SELECT Amount FROM   tb_amount m WHERE `Month`=1   AND m.`Year`=tb_amount.`Year`) AS m1,
20 (SELECT Amount FROM   tb_amount m WHERE `Month`=2   AND m.`Year`=tb_amount.`Year`) AS m2,
21 (SELECT Amount FROM   tb_amount m WHERE `Month`=3   AND m.`Year`=tb_amount.`Year`) AS m3,
22 (SELECT Amount FROM   tb_amount m WHERE `Month`=4   AND m.`Year`=tb_amount.`Year`) AS m4
23 FROM tb_amount  GROUP BY `Year`;
复制代码

 

 

 

 

 

 

posted on   宁静*勤奋  阅读(573)  评论(1)    收藏  举报
编辑推荐:
· 我在厂里搞 wine 的日子
· 如何通过向量化技术比较两段文本是否相似?
· 35+程序员的转型之路:经济寒冬中的希望与策略
· JavaScript中如何遍历对象?
· 领域模型应用
阅读排行:
· 独立项目运营一周年经验分享
· 独立开发,这条路可行吗?
· 文生图:介绍一个文字生成图片的开源工具
· Java简历、面试、试用期、转正
· MySQL 10 MySQL为什么有时候会选错索引?
< 2025年7月 >
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示