mysql 经典题目

时间:2023-03-08 21:20:52

题目1:实现如下效果

mysql 经典题目

 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`;